Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP更新相关数据_Cakephp_Cakephp 2.3 - Fatal编程技术网

CakePHP更新相关数据

CakePHP更新相关数据,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,在我的项目中,我有两个相关的模型:Company和CompanyComment。这家公司只有一家公司 现在,当我想通过saveAssociated更新公司信息时,我会按照以下方式进行: 控制器操作: public function edit($id = null){ $this->Company->id=$id; $this->Company->recursive = 0; if($this->

在我的项目中,我有两个相关的模型:Company和CompanyComment。这家公司只有一家公司

现在,当我想通过saveAssociated更新公司信息时,我会按照以下方式进行:

控制器操作:

    public function edit($id = null){

        $this->Company->id=$id;

        $this->Company->recursive = 0;      

        if($this->request->is('post')|| $this->request->is('put')){


            if($this->Company->saveAssociated($this->request->data)){
                $this->Session->setFlash(__('data has been updated'), 'positive_notification');
                $this->redirect(array('controller'=>'companies', 'action'=>'overview'));
            } else{
                $this->Session->setFlash(__('Data has not been updated. '), 'negative_notification');
            }
            }else{
            $this->request->data = $this->Company->read();
            }

    }
观点如下:

<?php echo $this->Form->create('Company', array('enctype' => 'multipart/form-data'));?>

<fieldset>

    <?php
        echo $this->Form->input('Company.newsletter');
        echo $this->Form->input('CompanyComment.comment_1', array('label'=>__('Domas comment'))); 
        echo $this->Form->input('CompanyComment.comment_2', array('label'=>__('Sunny comment')));
        ?>


    </fieldset>
<?php echo $this->Form->end(__('Update'));?>
但它无法保存数据。非常感谢您的帮助或指导

以下是公司模式关系:

var $hasOne = 'CompanyComment';
请注意,CompanyComment表的主键是company\u id,而不是自动递增的id。

试试这个

public function edit($id = null){

    $this->Company->id=$id;

    $this->Company->recursive = 0;     <--Remove this line

    if($this->request->is('post')|| $this->request->is('put')){

 //Change here saveAssociated to saveAll like following
        if($this->Company->saveAll($this->request->data)){
            $this->Session->setFlash(__('data has been updated'), 'positive_notification');
            $this->redirect(array('controller'=>'companies', 'action'=>'overview'));
        } else{
            $this->Session->setFlash(__('Data has not been updated. '), 'negative_notification');
        }
        }else{
        $this->request->data = $this->Company->read();
        }

}
你说:

请注意,CompanyComment表的主键是company_id,而不是自动递增的id

你在模特身上改了吗


感谢您的快速回复,但仍无法保存,并且会显示flash消息。如果我能提供任何其他重要信息,请告诉我。您是否在CompanyComment表中输入注释1和注释2?是的,当然可以,并且视图操作按预期工作,所有字段都被检索,等等。只是没有更新。我刚刚用一条注释更新了这个问题:CompanyComment表的主键是company_id,而不是自动递增的id字段。你调试过你的代码了吗?
public function edit($id = null){

    $this->Company->id=$id;

    $this->Company->recursive = 0;     <--Remove this line

    if($this->request->is('post')|| $this->request->is('put')){

 //Change here saveAssociated to saveAll like following
        if($this->Company->saveAll($this->request->data)){
            $this->Session->setFlash(__('data has been updated'), 'positive_notification');
            $this->redirect(array('controller'=>'companies', 'action'=>'overview'));
        } else{
            $this->Session->setFlash(__('Data has not been updated. '), 'negative_notification');
        }
        }else{
        $this->request->data = $this->Company->read();
        }

}
class CompanyComment extends AppModel {
    public $primaryKey = 'company_id';
}