7

每当插入或更新实体时,都会调用 afterSave 回调。如何检查实体是否是新插入的?

我在 afterSave 回调中尝试执行的操作的伪代码。

if newly inserted {
    dispatch event using the events system;
}
4

1 回答 1

16

我得到了答案。答案如下:

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

我希望这对 CakePHP 3 的新手有所帮助。

链接在这里:http ://api.cakephp.org/3.0/class-Cake.Datasource.EntityInterface.html#_isNew

于 2014-08-31T05:05:54.317 回答