doucao1888 2015-03-25 11:39
浏览 49
已采纳

列错误无效 - 必须存在于表中或是带别名的表达式(Yii 1.x)

In a model I have the following logic:

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

This has the following SQL query -

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

This query when run directly in the database works fine and returns the expected data. However the problem is when I run the criteria logic into my CAtiveDataProvider() i get the error.

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria
    ));

Can anyone suggest how to fix this to get it working inconjunction with my CActiveDataProvider?

  • 写回答

1条回答 默认 最新

  • douwen5546 2015-03-25 16:28
    关注

    Specify your select as follows:

    $criteria->select = array(
        'COALESCE(oca.reason, ocd.reason) AS reward_name',
        'SUM(t.points) AS points',
        'COUNT(t.redeemed_code_id) AS totalCount'
    );
    

    The reason why your code fails is that on line 917 of CActiveFinder (at least in Yii 1.14, which I use) uses the following logic for handling a string select statement:

    if(is_string($select))
        $select=explode(',',$select);
    

    That explode fails miserably on your select statement, given that you have commas within the expression. Format your select as an array, as shown above, to avoid this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示