Yii:查询构建器:按顺序表达

时间:2014-08-12 15:00:11

标签: php yii expression builder

在AC中,此顺序效果很好,但“查询”构建器的引号错误,结果错误

$models = Yii::app()->db->createCommand()
->select('id, user_id, title, created, modified, lang, forum_id, post_id, status, views, replies, attached')
->from('posts')
->where('post_id = 0')
->order('attached AND forum_id = 1 AND created DESC, created DESC')
->limit(11)
->queryAll();

错误:

  

CDbCommandнеудалосьисполнитьSQL-запрос:SQLSTATE [42S22]:未找到列:1054未知列'附加AND forum_id = 1并且'在'order子句'中创建'。执行的SQL语句是:SELECT iduser_idtitlecreatedmodifiedlangforum_id,{ {1}},post_idstatusviewsreplies   来自attached   WHERE post_id = 0   ORDER BY posts DESC,attached AND forum_id = 1 AND created DESC,replies DESC LIMIT 11

如何解决?

P.S。抱歉我的英文。

2 个答案:

答案 0 :(得分:2)

您的whereorder应该是:

->where('post_id = 0')
->order('CASE WHEN forum_id =1 THEN 1 ELSE 2 END, created DESC')

参考:How do I return rows with a specific value first?

答案 1 :(得分:0)

您应该在圆括号中使用顺序表达式以避免检查列。这必须工作

->order('(attached AND forum_id = 1 AND created) DESC, created DESC')