0

I'm trying to load, edit and save a record with CakePHP 2.0 but I get a generic error during the save method that don't help me to understand where is the problem.

if I try with debug($this->User->invalidFields()); I get an empty array, but I get false from $this->User->save() condition.

Here is the controller action where I get the error:

public function activate ($code = false) {
    if (!empty ($code)) {

        // if I printr $user I get the right user
        $user = $this->User->find('first', array('activation_key' => $code));

        if (!empty($user)) {
            $this->User->set(array (
                'activation_key' => null,
                'active' => 1
            ));

            if ($this->User->save()) {
                $this->render('activation_successful');
            } else {
                // I get this error
                $this->set('status', 'Save error message');
                $this->set('user_data', $user);
                $this->render('activation_fail');
            }
            debug($this->User->invalidFields());

        } else {
            $this->set('status', 'Account not found for this key');
            $this->render('activation_fail');
        }
    } else {
        $this->set('status', 'Empty key');
        $this->render('activation_fail');
    }
}

When I try the action test.com/users/activate/hashedkey I get the activation_fail template page with Save error message message.

If I printr the $user var I get the right user from cake's find method.

Where I'm wrong?

2
  • Is it possible that it's failing to validate? Nov 11, 2011 at 22:47
  • can you suggest me some alternative way to debug invalid fileds instead of debug($this->User->invalidFields());?
    – vitto
    Nov 11, 2011 at 22:49

1 Answer 1

5

I think the problem may be in the way you're querying for the User record. When you do this:

$user = $this->User->find('first', array('activation_key' => $code));

The variable $user is populated with the User record as an array. You check to ensure it's not empty, then proceed; but the problem is that $this->User hasn't been populated. I think if you tried debug($this->User->id) it would be empty. The read() method works the way you're thinking.

You could try using the ID from that $user array to set the Model ID first, like so:

if (!empty($user)) {
    $this->User->id = $user['User']['id']; // ensure the Model has the ID to use
    $this->User->set(array (
        'activation_key' => null,
        'active' => 1
    ));
    if ($this->User->save()) {
    ...

Edit: Well another possible approach is to use the $user array instead of modifying the current model. You said that you get back a valid user if you debug($user), so if that's true you can do something like this:

if (!empty($user)) {
    $user['User']['activation_key'] = null;
    $user['User']['active'] = 1;
    if ($this->User->save($user)) {
    ...

This method works in the same way as receiving form data from $this->request->data, and is described on the Saving Your Data part of the book.

I'm curious though if there's another part of your setup that's getting in the way. Can other parts of your app write to the database properly? You should also check to make sure you aren't having validation errors, like their example:

<?php
if ($this->Recipe->save($this->request->data)) {
    // handle the success.
}
debug($this->Recipe->validationErrors);
3
  • Thanks for the information, as you said the $this->User->id was empty, I've tried to fix it as you written but the problem sill the same, also with $this->User->save($this->User->data) without success. What can it be instead of the $this->User was empty?
    – vitto
    Nov 12, 2011 at 15:03
  • Added a few ideas above, vitto
    – brism
    Nov 12, 2011 at 19:26
  • Thank you again. The second part of your answer resolved my problem, I needed to add data to $user and then save it. I also needed to change $this->User->find('first', array('activation_key'=>$code)) to ...find('first', array('conditions' => array('User.activation_key' => $code)));
    – vitto
    Nov 13, 2011 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.