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

瘦欲@ 提交于 2019-12-12 10:38:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!