【发布时间】:2014-08-31 04:23:57
【问题描述】:
每当插入或更新实体时,都会调用 afterSave 回调。如何检查实体是否是新插入的?
我在 afterSave 回调中尝试执行的伪代码。
if newly inserted {
dispatch event using the events system;
}
【问题讨论】:
标签: cakephp cakephp-3.0
每当插入或更新实体时,都会调用 afterSave 回调。如何检查实体是否是新插入的?
我在 afterSave 回调中尝试执行的伪代码。
if newly inserted {
dispatch event using the events system;
}
【问题讨论】:
标签: cakephp cakephp-3.0
我已经得到了答案。答案如下:
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
【讨论】: