Cliff_Cleet
Cliff_Cleet

Reputation: 211

how to use Orderby in cakephp

I am trying to order data in accordance with its priority... But this query is not working for me.... I cannot figure it out where it went wrong..

please help me to solve this..

$admins = $this->xxx->find()
    ->select($fields)
    ->where($conditions)
    ->contain([
        'yyy' => function ($q) {   
            return $q->autoFields(false)
                ->select(['id','name','login_url','priority'])
                ->order(['priority' => 'ASC']);
            }
    ])
    ->all();

Upvotes: 0

Views: 3219

Answers (2)

Amit Vishwakarma
Amit Vishwakarma

Reputation: 1

array(
    'conditions' => array('Model.field' => $thisValue), //array of conditions
    'recursive' => 1, //int
    //array of field names
    'fields' => array('Model.field1', 'DISTINCT Model.field2'),
    //string or array defining order
    'order' => array('Model.created', 'Model.field3 DESC'),
    'group' => array('Model.field'), // fields to GROUP BY
    'limit' => n, //int
    'page' => n, //int
    'offset' => n, //int
    'callbacks' => true //other possible values are false, 'before', 'after'
    'having' => array('COUNT(Model.field) >' => 1), // fields to HAVING by
    'lock' => true // Enable FORM UPDATE locking
)

Upvotes: 0

arilia
arilia

Reputation: 9398

If the realtionship between xxx and yyy is belongsTo then you have to move the order() method outside the contain

$admins = $this->xxx->find()
    ->select($fields)
    ->where($conditions)
    ->contain([
        'yyy' => function ($q) {   
            return $q->autoFields(false)
                ->select(['id','name','login_url','priority']);
            }
    ])
    ->order(['yyy.priority' => 'ASC'])
    ->all();

Upvotes: 3

Related Questions