Model events
First, from the manual, we can know that the model supports the following events:
Event description event method name after_read after query onAfterRead before_insert before adding onBeforeInsert after_insert after adding onAfterInsert before_update before update after_update onAfterUpdate before_write before writing after_write onAfterWrite before_delete before deleting after_delete onAfterDelete before_restore onBeforeRestore after_restore onAfterRestore
<?php namespace app\model; use think\Model; use think\model\concern\SoftDelete; class Users extends Model { // Soft delete use SoftDelete; public static function onAfterRead($user) { dump('After query'); } public static function onBeforeInsert($user) { dump('Before adding'); } public static function onAfterInsert($user) { dump('After adding'); } public static function onBeforeUpdate($user) { dump('Before update'); } public static function onAfterUpdate($user) { dump('After update'); } public static function onBeforeWrite($user) { dump('Before writing'); } public static function onAfterWrite($user) { dump('After writing'); } public static function onBeforeDelete($user) { dump('Before deleting'); } public static function onAfterDelete($user) { dump('After deletion'); } public static function onBeforeRestore($user) { dump('Before recovery'); } public static function onAfterRestore($user) { dump('After recovery'); } }
method
$data = [ 'name' => 'test', 'phone' => '13888888888', 'area' => '0', 'address' => 'Shaoguan City, Guangdong Province', 'balance' => '0', 'password' => '0', 'status' => '0', 'last' => '2019-01-01 00:00:00', ]; dump('Users::create($data)'); $users = Users::create($data); dump('Users::insert($data)'); Users::insert($data); dump('Users::where(\'id\', $users)->update([\'area\' => 1])'); Users::where('id', $users['id'])->update(['area' => 1]); dump('$users->save([\'area\' => 2])'); $users->save(['area' => 2]); dump('Users::where(\'id\', $users[\'id\'])->delete()'); Users::where('id', $users['id'])->delete(); dump('Users::where(\'1=1\')->find()'); $users = Users::where('id', '>', 0)->find(); dump('Users::destroy($users[\'id\'])'); Users::destroy($users['id']); dump('$users->restore()'); $users->restore(); dump('$users->delete()'); $users->delete();
test
^ "Users::create($data)" ^ "Before writing" ^ "Before adding" ^ "After adding" ^ "After writing" ^ "Users::insert($data)" ^ "Users::where('id', $users)->update(['area' => 1])" ^ "$users->save(['area' => 2])" ^ "Before writing" ^ "Before update" ^ "After update" ^ "After writing" ^ "Users::where('id', $users['id'])->delete()" ^ "Users::where('1=1')->find()" ^ "After query" ^ "Users::destroy($users['id'])" ^ "After query" ^ "Before deleting" ^ "After deletion" ^ "$users->restore()" ^ "Before recovery" ^ "After recovery" ^ "$users->delete()" ^ "Before deleting" ^ "After deletion"
summary
create() The data creation method of the model will trigger before writing, before adding, after adding and after writing. Using the model's save() and saveAll() to add new methods also triggers these events. insert() insert() is a method of Db class, not a model method, and will not trigger model events. update() update() is a method of Db class, not a model method, and will not trigger model events. save() Using the save() method of the model to update the data will trigger events before writing, before updating, after updating and after writing. delete() If you use the model method to query the data and then delete the data, the pre delete and post delete events will be triggered. If the condition deletion is used directly, the model event will not be triggered. Because conditional deletion is used directly, the delete() method is not a model method at this time. find() This query method will trigger post query events destroy() The data deletion method will trigger after query, before deletion and after deletion. Therefore, the method is to query the data first, and then delete the data. restore() This soft delete recovery method will trigger the pre recovery and post recovery methods