将数据更新到CakePHP中另一个模型的关联模型

时间:2014-04-23 07:51:26

标签: cakephp

我创建了一堆代码,用于更新另一个模型的关联模型字段。

我有3个型号:

1. Review
2. Application
3. Stat

在我的ReviewsController.php

           foreach ($this->request->data['Review']['Mentee'] as $mentee) {

                $this->loadModel('Application');
                $this->Application->id = $mentee;
                pr($this->Application);
                $this->Application->Stat->saveField('second_video_observation_completed', $this->request->data['Review']['third_video_setting']);
            }

我正在尝试更新second_video_observation_completed模型

中的Stat字段

$this->Application

的pr输出

http://pastebin.com/jBYL8dzp

$this->Application->Stat

的pr输出

http://pastebin.com/iFD0nyjU

我通过DebugKit检查了sql日志,但是它插入了新记录而不是更新Stat Application w/ id 858INSERT INTO `ntc_development`.`stats` (`second_video_observation_completed`, `modified`, `created`) VALUES ('0', '2014-04-23 07:49:58', '2014-04-23 07:49:58') 记录。

{{1}}

1 个答案:

答案 0 :(得分:0)

saveField模型上使用Stat您需要为id模型而不是Stat模型设置Application

$this->Application->Stat->id = 'YOUR_ID';
$this->Application->Stat->saveField('second_video_observation_completed', $this->request->data['Review']['third_video_setting']);