【问题标题】:Failure to Update record CakePHP更新记录失败 CakePHP
【发布时间】:2014-04-02 21:35:34
【问题描述】:

我目前正在尝试在发送一封电子邮件之前更新我的用户,该电子邮件将包含一个带有重置密钥的确认链接,供用户重置密码。

问题是在生成密钥时用户没有更新。

代码见下

public function forgotpassword() {
        if ($this->request->is('post')) {
            $mail = $this->request->data['ContactDetail']['email'];
            $data = $this->User->find('first', array('conditions' => array('ContactDetail.email' => $mail)));

        if (!$data) {
            $message = __('No Such E-mail address registerd with us ');
            $this->Session->setFlash($message);
        } else {
            $data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
            debug($data);
            if ($this->User->saveAssociated($data)) {
                 $this->Session->setFlash(__('The user has been saved.'));
            }
            else {
                  $this->Session->setFlash(__('The user could not be updated. Please, try again.'));
            }
            $key = $data['User']['resetkey'];
            $id = $data['User']['id'];
            $mail = $data['ContactDetail']['email'];
            $email = new CakeEmail('default');
            $email->to($mail);
            $email->from("xxxx@yahoo.com");
            $email->emailFormat('html');
            $email->subject('Password reset instructions from');
            $email->viewVars(array('key' => $key, 'id' => $id, 'rand' => mt_rand()));
            $email->template('reset');
            if ($email->send('reset')) {
                $message = __('Please check your email for reset instructions.');
                $this->Session->setFlash($message);
            } else {
                $message = __('Something went wrong with activation mail. Please try later.');
                $this->Session->setFlash($message);
            }
        }
        $this->redirect('/');
    }
}

我检查了 MySQL 的执行日志,但我看到的只是选择不更新甚至插入。

这可能是非常愚蠢的事情,但我不明白为什么它什么也没说。

【问题讨论】:

  • 为什么要使用saveAssociated 方法更新单个字段?这样的事情会更容易:$this->User->updateAll(array('User.resetkey'=>$hash),array('User.id'=>$userId));
  • @walkingRed 您的评论足以回答我的问题,如果您愿意将其作为答案,我会接受。非常感谢

标签: cakephp cakephp-2.4


【解决方案1】:

为什么要使用saveAssociated 方法更新单个字段?

这样的事情会更容易:

$this->User->updateAll(
    array( 'User.resetkey' => $hash ),
    array( 'User.id' => $userId )
);

【讨论】:

    【解决方案2】:

    尝试使用

     $this->User->saveMany($data, array('deep' => true));
    

    保存时。还有

     if (empty($data)) {...
    

    可能是更安全的方式

    【讨论】:

      【解决方案3】:

      这对你有用-

      $this->User->id = $data['User']['id'];
      if ($this->User->save($data)) {
          $this->Session->setFlash(__('The user has been saved.'));
      }else {
          $this->Session->setFlash(__('The user could not be updated. Please, try again.'));
      }
      

      【讨论】:

        【解决方案4】:

        尝试调试如下:

                    if ($this->User->saveAssociated($data)) {
                         $this->Session->setFlash(__('The user has been saved.'));
                    }
                    else {
                          $this->Session->setFlash(__('The user could not be updated. Please, try again.'));
                    }
                    $log=$this->User->getDataSource()->getLog(false, false);
                    echo "<pre>";print_r($log);exit;
        

        【讨论】:

        • 我这样做了,实际上我提到过,我得到的只是选择语句。
        猜你喜欢
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        相关资源
        最近更新 更多