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?
I think the problem may be in the way you're querying for the User record. When you do this:
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 trieddebug($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: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 youdebug($user)
, so if that's true you can do something like this: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: