Question

I am currently trying to update my user before sending an email which will have a confirm link with a resetkey for the user to reset the password.

The problem is that the user is not updating whilst the key is being generated.

the code can be seen hereunder

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('/');
    }
}

I checked the execution log of MySQL but all I am seeing are selects no updates or even inserts.

This is probably something very dumb but I can't understand why it doesn't say anything.

Was it helpful?

Solution

Why do you use saveAssociated method for update single field?

Something like this would be easier:

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

OTHER TIPS

try using

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

When saving. Also

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

Might be a safer way to go

This will work for you-

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

Try to debug like:

            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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top