2014-01-12 48 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