Andreas Hinderberger
Andreas Hinderberger

Reputation: 1525

yii mutliple joins not showing all columns

I have the following createcommand:

$query = Yii::app()->db->createCommand()
    ->select('q.*, qcia.value, qcim.value')
    ->from('quotas q')
    ->leftjoin('quota_company_item_activations qcia', 'qcia.quota_id = q.id')
    ->leftjoin('quota_company_item_mrcs qcim', 'qcim.quota_id = q.id')
    ->where('q.id=:id', array(':id' => $quota_id))
    ->queryRow();

In the result array i should have twice a value field, but the array looks like that:

[id] => 3
[name] => September Sales Quotas
[company_id] => 1
[user_id] => 22
[datestart] => 2014-09-01
[dateend] => 2014-09-30
[created] => 2014-09-30 21:12:44
[modified] => 0000-00-00 00:00:00
[value] => 60.00

Why it only retrieves one value (from last leftjoin) ?

Upvotes: 1

Views: 66

Answers (2)

Manquer
Manquer

Reputation: 7647

You need to use an alias for one of the values.. php arrays are indexed by the keys.. the first value was overwritten by the second value with something like this

$query = Yii::app()->db->createCommand()
    ->select('q.*, qcia.value avalue, qcim.value mvalue') 
    ->from('quotas q')
    ->leftjoin('quota_company_item_activations qcia', 'qcia.quota_id = q.id')
    ->leftjoin('quota_company_item_mrcs qcim', 'qcim.quota_id = q.id')
    ->where('q.id=:id', array(':id' => $quota_id))
    ->queryRow();

Upvotes: 1

Prabowo Murti
Prabowo Murti

Reputation: 1341

Try using the alias so you can get different value columns.

$query = Yii::app()->db->createCommand()
->select('q.*, qcia.value AS qcia_value, qcim.value AS qcim_value')
->from('quotas q')
->leftjoin('quota_company_item_activations qcia', 'qcia.quota_id = q.id')
->leftjoin('quota_company_item_mrcs qcim', 'qcim.quota_id = q.id')
->where('q.id=:id', array(':id' => $quota_id))
->queryRow();

var_dump($query);

Upvotes: 0

Related Questions