我的问题很简单:应该如何形成的数据看起来像(E.I.又该名键的样子)使用patchEntities()
修补多个实体?
我已阅读Cookbook > Database Access & ORM > Saving Data > Converting Multiple Records,但它并没有明确提及如何使用它。
这种结构适用于newEntities()
:
$data = [
'0' => ['field1' => '...', /* ... */],
'1' => ['field1' => '...', /* ... */],
'2' => ['field1' => '...', /* ... */]
];
随着形式,如:
<?= $this->Form->input('0.field1', /* ... */) ?>
<?= $this->Form->input('1.field1', /* ... */) ?>
...
然而,同样的结构,但'id.field1'
不会对在patchEntities()
实体的任何变化。
表单数据应该是相同的,不同之处在于它应该包括记录的主键,以便编组可以映射在各自记录的数据。在表单输入的前导号码不存在主键(ID),但只是将所得数组索引。
该文档可以使用一个小更新,以包括有patchEntities()
过,现在这里只有很短的部分隐藏在“修补的hasMany和BelongsToMany”部分。
Cookbook > Database Access & ORM > Saving Data > Patching HasMany and BelongsToMany
所以,形式应该是这个样子:
<?= $this->Form->input('0.id', /* ... */) ?>
<?= $this->Form->input('0.field1', /* ... */) ?>
<?= $this->Form->input('1.id', /* ... */) ?>
<?= $this->Form->input('1.field1', /* ... */) ?>
<!-- ... -->
导致像的数据集:
$data = [
'0' => ['id' => '...', 'field1' => '...', /* ... */],
'1' => ['id' => '...', 'field1' => '...', /* ... */],
// ...
];
然后可以在给定的实体进行修补:
$original = $this->Table->find()->toArray();
$patched = $this->Table->patchEntities($original, $data);