不保存数据到2表有foreigenkey oncakephp

时间:2018-03-06 16:00:03

标签: php cakephp associations cakephp-2.x

关于项目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上的人_不能自动保存。 请帮助我!

1 个答案:

答案 0 :(得分: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);