CakePHP 3 - patchEntities() 的表单数据格式
CakePHP 3 - Form data format for patchEntities()
我的问题很简单:使用 patchEntities()
修补多个实体时,表单数据应该是什么样子(e.i。名称键应该是什么样子)?
我已阅读
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);
我的问题很简单:使用 patchEntities()
修补多个实体时,表单数据应该是什么样子(e.i。名称键应该是什么样子)?
我已阅读 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);