Yii - findAll 与 order by [英] Yii - findAll with order by
本文介绍了Yii - findAll 与 order by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用按 desc 排序的特定列查找所有内容?
How to findAll with specific column with order by desc ?
下面的代码有效并从开发者 id 中找到了所有内容
$id = Yii::app()->user->getState('id');
$models = Games::model()->findAll('developer_id='.$id);
代码波纹管工作和订购
$models = Games::model()->findAll(array('order'=>'status'));
当我混合在一起时,只对 findAll developer_id='.$id 不按顺序工作
$id = Yii::app()->user->getState('id');
$models = Games::model()->findAll('developer_id='.$id,array('order'=>'status'));
有什么建议可以这样做吗?谢谢
Any suggestion to do that ? Thanks
推荐答案
在你的模型中,添加这个函数:
In your model, add this function:
public function scopes() {
return array(
'bystatus' => array('order' => 'status DESC'),
);
}
现在你可以像这样进行查询:
Now you can do the query like this:
$models = Games::model()->bystatus()->findAll('developer_id='.$id);
======
奖励:您还可以在模型中添加此功能:
Bonus: You can also add this function in your model:
public function bydeveloper($devId) {
$this->getDbCriteria()->mergeWith(array(
'condition' => 'developer_id = '.$devId,
));
return $this;
}
现在你可以像这样进行查询:
Now you can do the query like this:
$models = Games::model()->bystatus()->bydeveloper($id)->findAll();
这篇关于Yii - findAll 与 order by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文