Cakephp: afterSave () for insert only?

I want to determine if a new record has been inserted or an old record has been added in afterSave (), but I have no idea how to do this.

I was thinking of writing a configuration variable in beforeSave () (where I can detect an insert if no identifier is set), but maybe this is not a safe way to do this.

Any ideas?

+4
source share
3 answers

You can do this using the afterSave function ($ model, $ created) .

Because the second argument to afterSave() is Boolean and will be true if a new line is created, it will give you false .

+8
source

passing the identifier (when updating the record) and saving it immediately after saving or saving can help. eg; $ this-> set ('id', $ this-> MyModule-> id);

0
source

You can verify this by writing the following lines of code here:

 public function afterSave(Event $event, Entity $entity, array $options) { if ($entity->isNew()) { // do whatever you need to do here. } } 
0
source

Source: https://habr.com/ru/post/1432800/


All Articles