cakephp 3 original data in afterSave

拜拜、爱过 提交于 2019-12-01 19:43:53

You can access the original values via Entity::getOriginal() or Entity::extractOriginal(). If you want to get all changed fields, combine the latter one with Entity::visibleProperties(), something like:

debug($entity->extractOriginal($entity->visibleProperties()));

This should return the original values of all changed fields.

See also

As of CakePHP 3.0.4, you can either use Entity::extractOriginal(), which will return the original value of any field, whether it has changed or not, or use Entity::extractOriginalChanged(), which will only return changed fields.

With this update, to reproduce the behaviour described in the accepted answer, you will thus need something like:

public function afterSave(Event $event, Entity $entity, $options)
{
    debug($entity->extractOriginalChanged($entity->visibleProperties()));
}

See CakePHP 3.0.4 Release Notes, stating:

EntityTrait::extractOriginal() now behaves consistently with extract(). Both methods now include all named properties [...] A new method extractOriginalChanged() can be used to extract only the original values of changed attributes.

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