doucai1901 2016-02-17 13:47
浏览 96

Yii2 - viaTable join返回错误

I am trying to write hasMany relationship, but I am getting this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near 
'[[licences.id = userKeys.licence_id]] = `licences`.`0` LEFT JOIN `userKeys` `use' at line 1

The SQL being executed was: 
SELECT COUNT(*) FROM `activityLogsUserActivity` 
LEFT JOIN `users` ON `activityLogsUserActivity`.`user_id` = `users`.`id` 
LEFT JOIN `licences` ON `activityLogsUserActivity`.[[licences.id = userKeys.licence_id]] = `licences`.`0` 
LEFT JOIN `userKeys` `userKeys` ON `licences`.`user_id` = `userKeys`.`user_id`

The code:

public function getKeys()
    {
         return $this->hasMany(UserKeys::classname(), ['user_id' => 'user_id'])
            ->select('licences.licenceName, userKeys.*')
            ->from(['userKeys' => UserKeys::tableName()])
            ->viaTable(Licences::tableName(), ['licences.id = userKeys.licence_id']);
    } 

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongne1560 2016-02-17 16:14
    关注
    ['licences.id = userKeys.licence_id']
    

    should be a key-value pair, like

    ['id' => 'licence_id']
    

    and it should not be necessary to declare the table name there, have a look at the docs

    http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#viaTable()-detail

    The link between the junction table and the table associated with $primaryModel. The keys of the array represent the columns in the junction table, and the values represent the columns in the $primaryModel table.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大