0

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:

  • Controller
  • Model
  • Behavior

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:

  • PRIOR TO THE SAVE
  • $this->request->data = $this->data = proper data from the form
  • $this->User->data = some default, unpopulated array

  • PRIOR TO THE SAVE when I use $this->User->create($this->request->data)

  • all three arrays contain raw form data

  • AFTER THE SAVE in either case

  • $this->request->data = $this->data = exactly as before
  • $this->User->data = the properly massaged data

Can anyone sort me out?

Don Drake

user2378787
  • 1
  • 1
  • 1
  • the data is not passed by reference (to the model), therefore they array inside the model will most likely be different from the controller one after modification. This is basic OOP. What exactly is your problem now? Note, that you just need to do `$this->request->data = $this->Model->data` in order to have the latest data back in the controller again (usually not necessary, though). – mark Sep 15 '13 at 17:49
  • To clarify one small point, $this-request-data is unchanged and the save is failing although there are no validation failures or other errors I can see. – user2378787 Sep 15 '13 at 18:07
  • Did you check `$this->Model->validationErrors`? It contains the errors. – mark Sep 15 '13 at 18:18
  • I did check for validation errors. – user2378787 Sep 15 '13 at 20:01
  • I did check for validation errors. Currently the save is returning the message "The user could not be saved. Please, try again." If I make my validation rule return true no matter what, and comment-out my behavior beforeSave, I still get the failure. It's not clear why the save is failing. – user2378787 Sep 15 '13 at 20:07
  • So, the unchanged nature of $this->request->data is normal. $this->Model->data is changing via my validation and beforeSave processes. That's what will be save, if I'm understanding. If I could only figure out why data that appears correct is returning this could-not-save message... – user2378787 Sep 15 '13 at 20:20
  • The problem is something with my User model. I have a Catalog model that uses the same behavior and validation which works fine. Thanks Mark for letting me know I was chasing a red-herring. – user2378787 Sep 15 '13 at 22:22
  • Yeah, the unchanged nature is normal/expected. – mark Sep 15 '13 at 22:48

1 Answers1

1

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.

BadHorsie
  • 12,971
  • 26
  • 105
  • 175