Yii - findAll with order by

前端 未结 3 1842
野性不改
野性不改 2021-01-14 06:56

How to findAll with specific column with order by desc ?

Code bellow worked and find all from the developer id

$id = Yii::app()->         


        
3条回答
  •  攒了一身酷
    2021-01-14 07:21

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

提交回复
热议问题