Uzumaki Naruto
Uzumaki Naruto

Reputation: 753

leftJoin is returning columns from first table only

The code is in EventUserTypes model

$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()

There is no error exept that it is only returning the columns of first table and not the joined table. Its been 2 hours and have waisted too much energy on what is going wrong ? Any idea ?

If I select * then it returns all columns of first table

and if do this

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

it only returns event_usertypes.user_type_id not the name from the joined table

Please help me out

Upvotes: 1

Views: 101

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521914

Try doing a direct DB query, to ensure that the usertypes table will be available in your query results:

$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();

Have a look at this SO question which was similar to yours. Also here is a link to the Yii documentation, in case this might help.

Upvotes: 1

Rom
Rom

Reputation: 9

Please try like this

$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();

Upvotes: 0

Related Questions