Saving data with cakephp won't work

2019-08-30 13:36发布

问题:

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?

回答1:

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