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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With