Cakephp 3用jon更新

时间:2015-02-03 20:31:15

标签: cakephp-3.0

在我的cakephp3中,我首先加入了

模型中的表格
 public function initialize(array $config)
{
    $this->belongsTo('Users', [
        'className'=>'Users',
        'foreignKey'=>'user_id',
    ]);
}

当我使用此代码从控制器更新时

 $query=$this->Leaves->query();
        $query->contain(['Users'])
        ->update()
        ->set(['admin_noti_status' => 0])
        ->where(array('Users.company_id' => 6))
        ->execute();

它显示了一个错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.company_id' in 'where clause'

但是此列存在于users表中。

1 个答案:

答案 0 :(得分:0)

仅在Mysql中支持更新语句,这就是CakePHP ORM不支持开箱即用的原因。 Instad您可以使用from方法或使用子查询。

使用from:

$query->update()
->from(['u' => 'users'])
->where(['u.company_id' => 6])
->andWhere(['u.id = Leaves.user_id'])