cakephp 3 afterSave 中的原始数据 [英] cakephp 3 original data in afterSave
问题描述
有没有办法在afterSave中访问原始数据?我想记录重要数据的更改.
Is there any way to access original data in afterSave? I would like to log the changes on important data.
使用 $entity->isNew() 我可以检查它是插入还是更新,但我怎样才能获得更改的内容?
With $entity->isNew() I could check if it was an insert or an update, but how can I get what changed?
推荐答案
您可以通过 Entity::getOriginal()
或 Entity::extractOriginal()
访问原始值>.如果要获取所有更改的字段,请将后一个与 Entity::visibleProperties()
结合起来,例如:
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.
另见
- http://api.cakephp.org/3.0/class-Cake.Datasource.EntityTrait.html#_extractOriginal
- http://api.cakephp.org/3.0/class-Cake.Datasource.EntityTrait.html#_getOriginal
- http://api.cakephp.org/3.0/class-Cake.Datasource.EntityInterface.html#_visibleProperties
这篇关于cakephp 3 afterSave 中的原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!