CakePHP 3有很多更新奇怪的行为

时间:2016-07-22 08:50:00

标签: cakephp orm cakephp-3.0 cakephp-3.x

我有JobsTable:

这是关系定义:

$this->hasMany( 'JobContracts', [ 
  'foreignKey' => 'job_id' 
] );

保存代码:

$entity = $this->patchEntity( $entity, $toSave, [
        'fieldList' => ['notes],
        'associated' => [ 
            'JobContracts' => ['fieldList' => ['id', 'checked']]
        ] 
    ] );

现在:
如果我把这些笔记放在fieldList中,那么JobContracts就不能正确保存 如果我删除fieldList,那么我可以正确保存它。

问题是为什么?我还需要控制基本模型字段。有什么建议吗?

我已经检查过:http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks

1 个答案:

答案 0 :(得分:1)

您还需要允许分配关联属性,而不仅仅是notes。如果不这样做,则永远不会在生成的实体上设置关联数据,因此不会保存。

检查您再次链接的文档,标签示例完全显示:

// Only allow changing the title and tags
// and the tag name is the only column that can be set
$entity = $this->patchEntity($entity, $data, [
    'fieldList' => ['title', 'tags'],
    'associated' => ['Tags' => ['fieldList' => ['name']]]
]);
$this->save($entity);

<强> http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks

所以,将job_contracts添加到字段列表中,你应该很好。