【问题标题】:CakePHP 3 hasMany update strange behaviourCakePHP 3 hasMany 更新奇怪行为
【发布时间】:2016-07-22 08:50:00
【问题描述】:

我有 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

【问题讨论】:

  • 请尽量避免使用“未正确保存”之类的描述,这可能意味着任何事情,而在编程世界中,一切都是为了准确。即使问题对于了解 CakePHP 内部结构的人来说可能很明显,但请始终尽可能具体地说明究竟会发生什么,以及您期望会发生什么。

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


【解决方案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 添加到字段列表中,你应该会很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2011-11-06
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多