Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp3 how to check if entity is a newly inserted in afterSave callback?

Whenever an entity is inserted or updated, the afterSave callback is invoked. How do I check whether the entity is a newly inserted one?

Pseudo code of what I'm trying to do inside the afterSave callback.

if newly inserted {
    dispatch event using the events system;
}
like image 232
Shawn Ang Avatar asked Aug 31 '14 04:08

Shawn Ang


1 Answers

I have got the answer. The answer is as followed:

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

I hope this helps someone who's new to CakePHP 3.

Link here: http://api.cakephp.org/3.0/class-Cake.Datasource.EntityInterface.html#_isNew

like image 91
Shawn Ang Avatar answered Dec 03 '22 15:12

Shawn Ang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!