问题
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;
}
回答1:
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
来源:https://stackoverflow.com/questions/25588930/cakephp3-how-to-check-if-entity-is-a-newly-inserted-in-aftersave-callback