tags:

views:

71

answers:

1

I am trying to update a password into two different models/tables in CakePHP. I can update it fine in the parent model, but not the second model.

Models:
Users (hasOne GameProfile) PK=id
Gameprofiles (belongsTo User) FK=user_id

Here is a stripped down version of my function in the Users_controller.php:

function updatepass() {
 if (!empty($this->data)) {
  $this->User->id = $this->Auth->user('id');

  $this->User->saveField('sha1password', $this->Auth->password($this->data['User']['newpass']));

  $this->User->Gameprofile->saveField('plainpassword', $this->data['User']['newpass']);
 }
}

When I execute the function, the users table is updated fine. But the gameprofile table is not updated, rather Cake does an insert.

SQL Query Log:

    1195 Query UPDATE `users` SET `sha1password` = 'e9443e9f5e1a07832aad1b2f84de1a666daf89b5'  WHERE `users`.`id` = 30
    1195 Query INSERT INTO `gameprofiles` (`plainpassword`) VALUES ('abc')

Is there a way to get CakePHP to do an update using saveField on a model with a belongsTo attribute?

I've tried various ways to refer to user_id before executing the second saveField, but just can't seem to find the winning combination.

Any help is greatly appreciated!

A: 

Self-answered!

Lacked the following:

$this->User->Gameprofile->id = $this->Auth->user('id');

And was missing an 'id' column in the Gameprofile table.

afrisch