2016-07-22 23 views
0

我有JobsTable:的CakePHP 3的hasMany更新奇怪的行爲

這是關係定義:

$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

+0

請儘量避免像「保存不正確」的描述,這可能意味着絕對的東西,並在編程世界它是所有關於是精確的。即使問題可能是那些知道CakePHP的內部人明顯的,請永遠是儘可能具體以什麼_exactly_情況,以及你所期待,而不是發生。 – ndm

回答

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字段列表,和你應該很好。