Cakephp 获取实体';使用关联保存后的脏字段

Cakephp 获取实体';使用关联保存后的脏字段,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我试图在应用程序中记录每个操作(插入/更新/删除),并且在保存实体后获取dirty和original值。问题是关联实体的所有值都返回为dirty,甚至is\u new标志设置为true,但实际上我正在更新。是什么导致这种行为?我如何避免这种行为 例如: $data = [ 'name' => $name, 'something' => $something, 'Table1' => [ 'id' => $id

我试图在应用程序中记录每个操作(插入/更新/删除),并且在保存实体后获取
dirty
original
值。问题是关联实体的所有值都返回为dirty,甚至
is\u 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.
}

我深入研究了实体中的
dirty()
函数,根据API,如果您没有明确要求它检查属性,那么它只会告诉您实体是否有任何dirty属性

这样做

$entity->dirty('title')告诉您磁贴是否脏,但正在运行
$entity->dirty()只会告诉您实体中是否有任何属性是脏的


您可能希望根据实体中的字段是否已更改使代码有条件

例如,您可能只希望在字段更改时验证字段:

// 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');

如果要更新条目,是否使用get或find来检索要更新的记录。是的,我正在使用
$this->get($id)
加载主实体。其他表中的关联实体未加载,我在数据数组中传递它们的ID,如您在示例中看到的,因此它们将被更新,而不是插入/复制。所有的节省都很好,只是无法获取关联实体的脏字段。问题是所有字段都标记为脏字段,但实际上只有部分字段被更新。