2014-01-12 46 views
0

我有這樣的:更新相關表中的蛋糕

$this->request->data['Person']['person_id'] = $this->Session->read('insertedPersonID'); 
$savedPerson = $this->Person->saveAll($this->request->data, array('validate'=>'first')); 

該更新被選擇的人排精然而像PersonColor和PersonParts其相關的表沒有更新,而不是插入新行。

我想蛋糕自動更新相關的表,以及只要主表的id,這將是對其他兩個表的外鍵,因爲這樣做提供:

$savedPerson = $this->Person->saveAll($this->request->data, array('validate'=>'first')); 

插入到人表和其他兩個相關的兩個表罰款。

如何更新其他兩張表?

編輯: 對於模型的關係:

角色模型:

public $hasMany = array(
    'PersonParts' => array(
    'className' => 'Part', 
    'foreignKey' => 'part_person_id' 
    ), 
    'PersonColors' => array(
    'className' => 'Color', 
    'foreignKey' => 'color_person_id' 
    ) 
); 

部分型號:

public $belongsTo = array(
    'PartPerson' => array(
    'className' => 'Person', 
    'foreignKey' => 'part_person_id' 
    ) 
); 

顏色型號:

public $belongsTo = array(
'ColorPerson' => array(
    'className' => 'Person', 
    'foreignKey' => 'color_person_id', 
    'conditions' => '', 
    'fields' => '', 
    'order' => '' 
    ) 
); 

編輯2

的$這 - 的var_dump>請求 - >數據

array(3){ 
    ["Person"]=>array(4){ 
     ["person_user_id"]=>string(1)"3" 
     ["person_name"]=>string(9)"Britney" 
     ["person_category_id"]=>string(2)"16" 
     ["visibility"]=>string(1)"1" 
     ["person_id"]=>string(1)"71" 
    } 
    ["PersonParts"]=>array(1){ 
     [0]=>array(3){ 
      ["part_name"]=>string(4)"hands" 
      ["quantity"]=>string(1)"2" 
      ["part_part_type_id"]=>string(1)"1" 
     } 
    } 
    ["PersonColors"]=>array(2){ 
     [0]=>array(4){ 
      ["color_name"]=>string(3)"blue" 
      ["test_field1"]=>string(1)"8" 
      ["test_field2"]=>string(1)"9" 
      ["position"]=>int(1) 
     } 
     [1]=>array(2){ 
      ["color_name"]=>string(5)"red" 
      ["position"]=>int(2) 
     } 
    } 
} 

注意:此的var_dump僅示出[ 「爲person_id」] =>串(1) 「71」 以下的人數組作爲添加的字段,使蛋糕做更新,而不是插入... person_id沒有顯示在這裏的PersonParts和PersonColors,因爲它不是這樣工作的。我應該通過什麼或者我應該如何對其他2個相關表格進行更新?

+0

可以粘貼與Person,Personcolor和PersonParts模型中的關係有關的代碼嗎? – cornelb

+0

@cornelb好吧,我在「編輯」後發佈了關於問題的模型關係 – Leah

+0

您可以在保存之前發佈$ this-> request-> data的var_dump嗎? –

回答

0

爲了讓Cake對相關表進行更新,您需要傳入相關記錄的ID,否則Cake不會知道要更新哪個記錄。

你可以將它作爲你窗體中的一個隱藏字段,這是我做事的方式。

爲PersonParts的VAR轉儲應該是這樣的(注意額外的「PART_ID」字段):

["PersonParts"]=>array(1){ 
    [0]=>array(3){ 
     ["part_id"]=>string(1)"7" 
     ["part_name"]=>string(4)"hands" 
     ["quantity"]=>string(1)"2" 
     ["part_part_type_id"]=>string(1)"1" 
    } 
} 

如果不通過ID,然後(從內存中)蛋糕將刪除現有的相關記錄並添加新的。

+0

我加了$ this-> request-> data ['PersonColors'] [$ key] ['color_id'] = 92;並顯示在PersonColors的第一個陣列下,但仍未更新該行 – Leah

+0

您是否已在Color模型中正確設置primaryKey? –

+0

是的,我有。像這樣:public $ primaryKey ='color_id'; – Leah