【问题标题】:cakephp3 how to check if entity is a newly inserted in afterSave callback?cakephp3 如何检查实体是否是新插入的 afterSave 回调?
【发布时间】:2014-08-31 04:23:57
【问题描述】:

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

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

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

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 2017-12-21
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多