2015-07-20 26 views
1

的代码是EventUserTypes模型leftJoin从第一个表中列返回仅

$this->find() 
     ->select(['event_usertypes.user_type_id' , 'usertypes.name']) 
     ->leftJoin('usertypes' , 'event_usertypes.user_type_id = usertypes.id') 
     ->where(['event_usertypes.event_id'=>$event_id]) 
     ->all() 

没有错误exept,它只是返回第一个表 而不是连接表的列。它已经过了2个小时,并且在发生问题的时候放弃了太多精力?任何想法 ?

如果我选择*然后返回第一个表

的所有列,如果做到这一点

select(['event_usertypes.user_type_id' , 'usertypes.name']) 

它只返回event_usertypes.user_type_id不是这个名字从连接表

请帮助我出

回答

1

尝试做一个直接的数据库查询,以确保usertypes表将在您的查询结果中可用:

$query = new \yii\db\Query; 
$query->select(['e.user_type_id', 'u.name']) 
     ->from('event_usertypes e') 
     ->leftJoin('usertypes u', 'e.user_type_id = u.id') 
     ->where(['e.event_id'=> $event_id]); 
$command = $query->createCommand(); 
$resp = $command->queryAll(); 

看看this SO question这与您的相似。这里也是一个链接到Yii documentation,万一这可能有所帮助。

+0

都能跟得上什么也没你为什么要使用'找到()'而不是'EventUserTypes ::找到改变 –

+0

()'在你的代码? –

+0

另一个问题:你能否使用原始MySQL成功执行你想要的查询? –

0

请尝试这样

$query = new \yii\db\Query; 
$query->select(['event_usertypes.user_type_id' , 'usertypes.name']) 
     ->from('event_usertypes') 
     ->leftJoin('usertypes' , 'event_usertypes.user_type_id = usertypes.id') 
     ->where(['event_id'=> $event_id])->all(); 
$query->createCommand(); 
相关问题