1

I when I save (update) a field in a MySQL database via Model->save(), if I do not specify a value for a field that has a default value set then it gets reset to the default.

This is doing this even if there is already a value for that field.

For example, consider this code:

$existing_user = $this->CustomCart->User->field('id',array('User.email'=>$this->data['User']['email']));
if($existing_user)
{
    //update information
    $this->CustomCart->User->id = $existing_user;
}
$this->CustomCart->User->save($this->data);

In this code, it checks to see if a user already exists with the passed email address. If so, update that user with the passed information, else create a new user.

However I have a field in the users table called role the default value is customer

There is no value set for role in $this->data so when creating a new user the role gets automatically set to customer.

However, when updating an existing user, even if they have something else for role like "admin", it still resets the value to customer.

I do not know why this is. Other fields that I do not specifically set (that do not have a default value) maintain their values. So why do fields with default values get reset?

JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

2 Answers2

5

I got this one! You need to set this before save:

$this->CustomCart->User->create(false);

Without false Cake somehow guesses how model should look like.

Henri
  • 740
  • 10
  • 22
0

I'll suppose that field() is a function that you've created to get a single field according to some conditions

how about this code?:

$existing_user = $this->CustomCart->User->field('id',array('User.email'=>$this->data['User']['email']));
if($existing_user !== false){
    //update information
    $this->CustomCart->User->id = $existing_user;
    $this->data['User']['role'] = "admin";
}
$this->CustomCart->User->save($this->data);

i'd be really amazed if this code is not working correctly

Good Luck!

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
  • Thanks. Actually `field()` is a part of the framework, but it does do what you supposed. This solution would fix my current problem, however if I add more default fields in the future I'll have the same issues again. I would rather find the root cause of the problem so I can not only fix it for this particular case, but make sure it doesn't start happening elsewhere in the future as well. – JD Isaacks Jun 16 '11 at 14:43
  • the problem is simple, and is not actually a problem. You're trying to save a user from the CustomCart controller, so you have to set all the values in the `$this->data['User']`.. my guess is that you're not constructing correctly your $this->data variable.. do a `print_r()` so we could see the data – pleasedontbelong Jun 16 '11 at 15:14
  • I am not specifying `$this->data['User']['role']`. Why would I? the MySQL default value is *customer* which is exactly what I want it to be for a **new** user. However I want existing user's *role* to remain whatever they are already set to (*admin*, *teammamber*,*unicorn*, etc.). If its already set, why would it reset to *customer*. That doesn't make any sense and is not the way MySQL is supposed to behave. When you do an UPDATE you do not have to specify every field, just the ones you want to change. Btw, I am not trying to be rude here, I just think we are not on the same page. – JD Isaacks Jun 16 '11 at 16:59
  • in that case... it would be helpful to see the code where you do the update, the `pr($this->data)` and the generated query.. it's not normal that cake always resets the value to it's default – pleasedontbelong Jun 16 '11 at 20:38
  • @pleasedontbelong, Cake usually only displays the generated query if there is an error. Do you know how to display the generated query otherwise? I did a Google search and I looked up the Model API and didn't see anything about it. – JD Isaacks Jun 17 '11 at 13:26
  • check this http://stackoverflow.com/questions/3647065/how-can-i-see-cakephps-sql-dump-in-the-controller – pleasedontbelong Jun 17 '11 at 16:12