无法更新记录CakePHP

时间:2014-04-02 21:35:35

标签: cakephp cakephp-2.4

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

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

下面可以看到代码

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的执行日志,但我看到的只是选择没有更新甚至插入。

这可能是非常愚蠢的事情,但我无法理解为什么它没有说什么。

4 个答案:

答案 0 :(得分:1)

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

这样的事情会更容易:

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

答案 1 :(得分:0)

尝试使用

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

保存时。还

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

可能是一种更安全的方式

答案 2 :(得分:0)

这对你有用 -

$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.'));
}

答案 3 :(得分:0)

尝试调试:

            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;