简体   繁体   中英

cakephp SQL error at $this->User->saveField()

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM