无法更新记录CakePHP
我目前正在尝试在发送电子邮件之前更新我的用户,该电子邮件将有一个带有resetkey的确认链接,供用户重置密码 问题是在生成密钥时用户没有更新 代码如下所示无法更新记录CakePHP,cakephp,cakephp-2.4,Cakephp,Cakephp 2.4,我目前正在尝试在发送电子邮件之前更新我的用户,该电子邮件将有一个带有resetkey的确认链接,供用户重置密码 问题是在生成密钥时用户没有更新 代码如下所示 public function forgotpassword() { if ($this->request->is('post')) { $mail = $this->request->data['ContactDetail']['email']; $d
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的执行日志,但我看到的只是没有更新,甚至没有插入
这可能是非常愚蠢的事情,但我不明白为什么它什么也没说。尝试使用
$this->User->saveMany($data, array('deep' => true));
储蓄的时候。也
if (empty($data)) {...
可能是一种更安全的方式这对你很有用-
$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.'));
}
试着像这样调试:
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;
if($this->User->saveAssociated($data)){
$this->Session->setFlash(_u('用户已保存');
}
否则{
$this->Session->setFlash(_u('无法更新用户。请重试');
}
$log=$this->User->getDataSource()->getLog(false,false);
回声“;打印(日志);出口
$this->User->updateAll(
array( 'User.resetkey' => $hash ),
array( 'User.id' => $userId )
);
为什么要使用saveAssociated
方法更新单个字段
这样做会更容易:
为什么要使用
saveAssociated
方法更新单个字段?这样做会更简单:$this->User->updateAll(数组('User.resetkey'=>$hash),数组('User.id'=>$userId))
@walkingRed您的评论足以回答我的问题,如果您将其作为答案,我将接受。感谢洛蒂这么做了,我实际上提到了我得到的只是精选语句。