更新相关表在蛋糕 [英] updating related tables in cake

查看:133
本文介绍了更新相关表在蛋糕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

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

更新所选人员行,但是其相关的表(如PersonColor和PersonParts)不会更新,新的行。

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

我认为蛋糕会自动更新相关的表,以及主表的id是其他两个表的外键。这样做:

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'));

插入Person表,其他两个相关的两个表格。

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?

编辑
模型关系:

For the model relations:

人员模型:

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

$ this-> request-> data

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

注意:这个var_dump只显示[person_id] => string(1)71下的Person数组作为添加字段做蛋糕做更新, .. person_id没有显示在PersonParts和PersonColors这里,因为它不工作的方式。我应该通过什么,或者如何对其他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?

推荐答案

相关的表,你需要传入相关记录的id,否则Cake不知道要更新哪个记录。

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.

PersonParts的var转储应该看起来像这样(注意额外的part_id字段):

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

如果不传递id,那么(从内存)Cake将删除现有的相关记录,并添加新的记录。

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

这篇关于更新相关表在蛋糕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆