2016-02-17 2 views
1

Я пытаюсь написать hasMany отношения, но я получаю эту ошибку:Yii2 - viaTable соединение возвращает ошибку

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` 

Код:

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']); 
    } 

Что я делаю неправильно?

+0

Попробуйте изменить '['licences.id = userKeys.licence_id']' to '['licences.id' => 'userKeys.licence_id']' – jagsler

ответ

0
['licences.id = userKeys.licence_id'] 

должна быть пара ключ-значение, как

['id' => 'licence_id'] 

и оно не должно быть необходимым объявить имя таблицы там, есть взгляд на документы

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.

Смежные вопросы