【问题标题】:Cakephp save inserting record instead of updatingCakephp 保存插入记录而不是更新
【发布时间】:2015-04-14 05:40:09
【问题描述】:

我正在尝试关注 cakephp '保存数据' 2x 食谱,但我很难过。我的 UserController 有一个功能 Patient。我的表是患者信息。下面是我用来保存的代码的 sn-p:

if (isset($this->request->data['patientinformation'])) {

       $this->patientinformation->create();
       $this->request->data['patientinformation']['UserID']=$this->Auth->user('id');
       $this->request->data['patientinformation']['Name']=$this->Auth->user('Name');

        } 
        $this->patientinformation->id['patientinformation']['id'];
        if ($this->patientinformation->save($this->patientinformation->id)){
           $this->Session->setFlash(__('Your information has been saved.'));
           $this->redirect(array('action' => 'index'));
        } else {

            $this->Session->setFlash(__('Your information could not be saved. Please, try again.'));

如果 id 存在,我希望做的是更新记录。我无法保存我的表单数据。如果我需要在此处添加更多信息,请告诉我。

谢谢。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    您确定您的数据正确保存在数据库中???

    您的代码中的以下这行没有任何意义:

    $this->patientinformation->id['patientinformation']['id'];
    

    使用这个:

    $this->patientinformation->id = $this->request->data['patientinformation']['id'];
    

    在您的代码$this->patientinformation->save($this->patientinformation->id) 的这一部分中,您试图保存ID,但它的方式完全不正确。您应该保存如下:

     $this->patientinformation->save($this->request->data['patientinformation']);
    

    【讨论】:

    • 谢谢曼瓦尔!它现在正在节省。不幸的是,它仍在插入新行。我的桌子上没有主键。我需要使用一个吗?
    • 太棒了。 @shred_the_red 如果你得到你需要的,你可以接受答案。
    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 2014-12-16
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多