Yii - findAll with order by

前端 未结 3 1808
野性不改
野性不改 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();
    
    0 讨论(0)
  • 2021-01-14 07:24

    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

    0 讨论(0)
  • 2021-01-14 07:35

    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);
    
    0 讨论(0)
提交回复
热议问题