user2378787
user2378787

Reputation: 1

CakePHP data change during validation and beforeSave is not being save with the changes

I'm saving data sent from a form.

In the Controller I am doing :

$this->User->create();
$this->User->save($this->request->data)

The $this->request->data looks like this:

'User' => array(
    'password' => '*****',
    'username' => 'ddddd',
    'role' => '256/aa01bdf80d42beb48dd3225acddf447fdd7a39f3',
    'parent_id' => '0/b6ba9bd57f6c70cf738891d4c6fac22abed4161d'
)

There are validation rules that works on 'role' and 'parent_id' to insure the role/parent ids are among those the user can access.

The validation changes the field values if the data is valid.

I also have a Tree behavior that is setting some tree fields in a beforeSave() filter in the behavior.

The validation rule is writing the change to $this->data->[$model][$field] as shown below.

public function checkListHash($check, $field) {
    $explodedCheck = explode('/', $check[$field]);
    if ($this->secureId($explodedCheck[0], $explodedCheck[1])) {
        $this->data['User'][$field] = $explodedCheck[0];
        return true;
    }
    return false;
}

The beforeFilter() in the behavior is changing the data array with statements like this:

$Model->data[$Model->alias][$ancestors] = $ancestorList;

When validation and the beforeFilter() processing is complete, I have a beautiful and correct array of data at $this->User->data that looks like this:

'User' => array(
    'password' => '*****',
    'active' => '0',
    'role' => '256',
    'parent_id' => '0',
    'node' => '0',
    'username' => 'ddddd',
    'modified' => '2013-09-15 09:55:02',
    'created' => '2013-09-15 09:55:02',
    'ancestor_list' => ',0,'
)

However, $this->request->data is unchanged. And that is what is being save.

Clearly I'm not understanding the relationship of these various ways to get to the data. I've tried a variety of ways to address the data in the three contexts:

And I've tried $this->User->create($this->request->data); before the Controller save() statement.

In the controller, what I'm seeing as available data arrays:

Can anyone sort me out?

Don Drake

Upvotes: 0

Views: 2915

Answers (1)

BadHorsie
BadHorsie

Reputation: 14544

Just to explain the data arrays to you, when you submit the form, the data from it is stored in $this->request->data on the controller. You are then modifying $this->User->data from inside the model, which is a different array on the model itself. It would not affect $this->request->data because it's a completely different array which belongs to the controller, and the model has no knowledge of it.

You are then saving the User model using the request data, which remains unchanged from when the form was submitted. This is logical and normal behaviour because you're not actually using the $this->User->data array that you've modified.

Your save might always be failing because the data the model is trying to save isn't the updated data, it's just the basic data from $this->request->data.

Try this:

$this->User->set($this->request->data);
$this->User->save();

Also, if you are using a beforeSave in your model, make sure the method returns true, or it will never actually go on to save.

Upvotes: 1

Related Questions