updating related tables in cake

巧了我就是萌 提交于 2019-12-25 06:58:41

问题


I have this:

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

which updates the selected person row fine however its related tables like PersonColor and PersonParts are not updating, instead inserting new rows.

I thought cake automatically updates the related tables as well as long as the id of the main table which is the foreign key for the other two tables are provided since doing this:

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

inserts to the Person table and the other two related two tables fine.

How do I make it update the other two tables as well?

Edit: For the model relations:

Person Model:

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

Part Model:

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

Color Model:

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

edit 2

var_dump of $this->request->data

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)
        }
    }
}

Note: This var_dump is only showing ["person_id"]=>string(1)"71" under Person array as the added field to make cake do an update, not insert... person_id is not showing under the PersonParts and PersonColors here since it's not working that way. What should I pass or How should I do an update on the other 2 related tables?


回答1:


To make Cake do an update on the related tables, you do need to pass in the id of the related record, otherwise Cake won't know which record to update.

You can make this a hidden field in your form, which is how I do things.

The var dump for PersonParts should look like this (note the extra 'part_id' field):

["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"
    }
}

If you don't pass the id, then (from memory) Cake will delete the existing related records, and add new ones.



来源:https://stackoverflow.com/questions/21079057/updating-related-tables-in-cake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!