【发布时间】: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