我正在尝试记录应用程序中的每个操作(插入/更新/删除),我通过在保存实体后获取dirty
和original
值来执行此操作。问题是关联实体的所有值都返回为脏,甚至is_new
标志设置为true
但实际上我正在更新。导致这种行为的原因是什么,我该如何避免?
示例:
$data = [
'name' => $name,
'something' => $something,
'Table1' => [
'id' => $idWhereUpdatingTable1,
'field1' => $field1,
'field2' => $field2,
],
'Table2' => [
'id' => $idWhereUpdatingTable2,
'field3' => $field3,
'field4' => $field4,
],
];
$options = ['associated' => ['Table1', 'Table2']];
$updatedEntity = $this->patchEntity($entity, $data, $options);
$save = $this->save($updatedEntity);
// Successfully logging the changes in the main entity
// Trying to log the changes in the associated entities
foreach($save->table1 as $entity)
{
// everything here is set to dirty (even ID field but it's not an insert) and I'm not able to fetch the updated fields only. Also getOriginal() doesn't return the old values.
}
答案 0 :(得分:0)
我做了一些挖掘实体中的dirty()
函数,并根据API,如果你没有明确要求它检查一个属性,那么它只会告诉你实体是否有任何脏属性。
这样做
$entity->dirty('title');
告诉您瓷砖是否脏,但运行$entity->dirty();
只会告诉您实体中的任何属性是否脏。
http://api.cakephp.org/3.1/class-Cake.ORM.Entity.html#_dirty
答案 1 :(得分:0)
您可能希望根据实体中的字段是否更改来使代码成为条件。
例如,您可能只想在字段更改时对其进行验证:
// See if the title has been modified. CakePHP version 3.5 and above
$entity->isDirty('title');
// CakePHP 3.4 and Below use dirty()
$entity->dirty('title');