CakePHP: Need help using saveField to update a fields in a belongsTo model

Posted by afrisch on Stack Overflow See other posts from Stack Overflow or by afrisch
Published on 2010-07-16T19:53:49Z Indexed on 2011/11/16 9:52 UTC
Read the original article Hit count: 232

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about php

Related posts about cakephp