【问题标题】:not save data to 2 table have foreigenkey oncakephp不将数据保存到 2 个表有foreigenkey oncakephp
【发布时间】:2018-03-06 16:00:03
【问题描述】:

关于 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 people_id 不自动保存。 请帮帮我!

【问题讨论】:

标签: php cakephp associations cakephp-2.x


【解决方案1】:

我记得您需要将此添加到您的 VisaPerson 模型中:

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

之后你只需要保存 VisaPerson 两个表都会被填满(如果$this-&gt;request-&gt;data['VisaProcess']$this-&gt;request-&gt;data['VisaPerson'] 存在):

$this->VisaPerson->save($this->request->data);

【讨论】:

  • @Hongnguyen try $this-&gt;VisaPerson-&gt;saveAll($this-&gt;request-&gt;data); %-) 你用的是什么蛋糕版本?
  • 我使用的是蛋糕版本 2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 2013-01-26
相关资源
最近更新 更多