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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配