在模型中,我有以下逻辑:
$criteria = new CDbCriteria();
$criteria->select = 'COALESCE(oca.reason, ocd.reason) AS reward_name, SUM(t.points) AS points, COUNT(t.redeemed_code_id) AS totalCount';
$criteria->join = 'LEFT JOIN organisation_classroom_deductions ocd ON ocd.deduction_id = t.order_product_id AND t.type = 1
LEFT JOIN organisation_classroom_achievements oca ON oca.achievement_id = t.order_product_id AND t.type = 2
LEFT JOIN myuser ON (myuser.user_id = t.myuser_id)';
$criteria->condition = 'myuser.organisation_id ='. user()->data->organisation->organisation_id;
$criteria->group = 't.order_product_id, t.type';
这有以下SQL查询 -
SELECT COALESCE (oca.reason, ocd.reason) AS reward, SUM(t.points) AS points, COUNT(t.redeemed_code_id) AS totalCount
FROM
`organisation_classroom_redeemed_codes` `t`
LEFT JOIN organisation_classroom_deductions ocd ON ocd.deduction_id = t.order_product_i AND t.type = 1 LEFT JOIN organisation_classroom_achievements oca ON oca.achievement_id = t.order_product_id AND t.type = 2 LEFT JOIN myuser ON (myuser.user_id = t.myuser_id)
WHERE myuser.organisation_id = 37383
GROUP BY t.order_product_id, t.type
直接在数据库中运行时,此查询正常工作并返回预期的数据。但是问题是当我将条件逻辑运行到我的CAtiveDataProvider()中时,我得到了错误。
return new CActiveDataProvider($this, array(
'criteria' => $criteria
));
有人可以建议如何解决这个问题,让它与我的CActiveDataProvider一起工作吗?
答案 0 :(得分:0)
按如下方式指定您的选择:
$criteria->select = array(
'COALESCE(oca.reason, ocd.reason) AS reward_name',
'SUM(t.points) AS points',
'COUNT(t.redeemed_code_id) AS totalCount'
);
您的代码失败的原因是CActiveFinder的第917行(至少在我使用的Yii 1.14中)使用以下逻辑来处理字符串select语句:
if(is_string($select))
$select=explode(',',$select);
由于你在表达式中有逗号,所以爆炸在你的select语句中失败了。如上所示,将select格式化为数组,以避免此问题。