开发者

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 wa开发者_运维知识库y to tell cake, that the attribute is related to a UserGroup?

Thanks in advance, EL


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜