views:

47

answers:

2

Hey guys.

When I try to update a user, let's say his account balance

$this->User->id = $validUserId;
$this->User->saveField('balance', 100);

I get this error

SQL Error: 1054: Unknown column 'User.group_id' in 'field list'

with this automatically generated query druring the save process

SELECT `User`.`group_id` FROM `users` AS `User`   WHERE `User`.`id` = *validUserId*    LIMIT 1

The user belongsTo a UserGroup and therefore the user has user_group_id attribute, is there any way to tell cake, that the attribute is related to a UserGroup?

Thanks in advance, EL

+1  A: 

Most certainly, the models should look like this:

class UserGroup extends AppModel {
  var $belongsTo = array(
    'UserGroup' => array(
      'className' => 'UserGroup',
      'foreignKey' => 'user_group_id'
    )
  );
}

class User extends AppModel {
  var $hasMany = array(
    'User' => array(
      'className' => 'User',
      'foreignKey' => 'user_group_id'
    )
  );
}

Read about associating CakePHP models in the book

PawelMysior
are you suer UserGroup belongsTo UserGroup and User hasMany Users? My model is fine...User belongsTo UserGroup, foreign key set to user_group_id and UserGroup hasMany Users, with the same FK
ELwhis
A: 

Maybe, if you are just saving a field, you don't need to do validations. In that case just do

$this->User->saveField('balance', 100, false);

http://api.cakephp.org/class/model#method-ModelsaveField

Or if there is a callback like beforeSave or something, I think you can do something like:

$this->save($data, array('validate'=>false, 'callbacks'=>false));

Hope this helps

jimiyash
disabling the callbacks solved the problem, thanks ;)
ELwhis