不保存数据到2表在cakephp中有外键

问题描述 投票:1回答:1

关于项目cakephp。我有2个型号

<?php class VisaPerson extends AppModel {
public $name = 'VisaPerson';
public $primaryKey  = 'id';}?>

<?php class VisaProcess extends AppModel {
    public $name = 'VisaProcess';
    public $primaryKey  = 'id';
    public $belongsTo = array(
            'VisaPerson' => array (
                    'className' => 'VisaPerson',
                    'foreignKey' => 'people_id'
            )
    );
}
?>

在控制器中,我写道:

    if ($this->request->is('post')) {
        if (!empty($this->request->data)) {
           $person = $this->VisaPerson->save($this->request->data);
           if (!empty($person)) {
               $this->request->data['VisaProcess']['people_id'] = $this->$person->id;
               $this->VisaPerson->VisaProcess->save($this->request->data);
        }
    }

保存在VisaPerson上的数据,但是VisaProcess上的数据不能自动保存。请帮助我!

php cakephp associations cakephp-2.x
1个回答
0
投票

我记得你需要将它添加到你的VisaPerson模型中:

public $hasMany = array(
    'VisaProcess' => array(
        'className'  => 'VisaProcess',
        'foreignKey' => 'people_id'
    )
);

之后你只需要保存VisaPerson并且两个表都将被填充(如果存在$this->request->data['VisaProcess']$this->request->data['VisaPerson']):

$this->VisaPerson->save($this->request->data);
热门问题
推荐问题
最新问题