php Yii - findAll 与 order by

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26964641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:07:06  来源:igfitidea点击:

Yii - findAll with order by

phpyii

提问by TheSmile

How to findAll with specific column with order by desc ?

如何使用按 desc 排序的特定列 findAll ?

Code bellow worked and find all from the developer id

代码波纹管工作并从开发人员ID中找到所有内容

$id = Yii::app()->user->getState('id');
$models = Games::model()->findAll('developer_id='.$id);

Code bellow worked and ordered

代码波纹管工作和订购

$models = Games::model()->findAll(array('order'=>'status'));

When I mixed together then only worked for findAll developer_id='.$id doesn't order by

当我混合在一起时,仅适用于 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

有什么建议可以这样做吗?谢谢

回答by Samuel Liew

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();

回答by Milap Jethwa

you can try this -

你可以试试这个——

$id = Yii::app()->user->getState('id');

$model = Games::model()->findAll(array("condition" => "developer_id = '".$id."'","order" => "status"));

its should be work

它应该是工作

回答by wawancell

You can try use criteria:

您可以尝试使用标准:

$id = Yii::app()->user->getState('id');
$criteria=new CDbCriteria;
$criteria->compare('developer_id',$id);
$criteria->order='status DESC';

$models = Games::model()->findAll($criteria);