yii多个连接不显示所有列

时间:2014-10-05 02:53:53

标签: php activerecord yii

我有以下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();

在结果数组中,我应该有两倍的值字段,但数组看起来像这样:

[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

为什么它只检索一个值(来自最后一个左连接)?

2 个答案:

答案 0 :(得分:1)

你需要为其中一个值使用别名.. php数组由键索引..第一个值被第二个值覆盖 用这样的东西

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

答案 1 :(得分:0)

尝试使用别名,这样您就可以获得不同的value列。

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