Enzero
Enzero

Reputation: 1141

Failure to Update record CakePHP

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("[email protected]");
            $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.

Upvotes: 0

Views: 140

Answers (4)

Tomasz Ferfecki
Tomasz Ferfecki

Reputation: 1263

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 )
);

Upvotes: 1

Er.KT
Er.KT

Reputation: 2860

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;

Upvotes: 0

Fazal Rasel
Fazal Rasel

Reputation: 4526

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

Upvotes: 0

GummyBearScientist
GummyBearScientist

Reputation: 1

try using

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

When saving. Also

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

Might be a safer way to go

Upvotes: 0

Related Questions