Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 3 original data in afterSave

Is there any way to access original data in afterSave? I would like to log the changes on important data.

With $entity->isNew() I could check if it was an insert or an update, but how can I get what changed?

like image 970
rrd Avatar asked Dec 15 '22 19:12

rrd


1 Answers

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

  • 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
like image 177
ndm Avatar answered Jan 10 '23 10:01

ndm