Duke
Duke

Reputation: 36970

Order by field in cakephp

I am doing project in cakephp .

I want to write below query in cakephp Style. I've written 50% . Please help me

$this->Login->find('all')

SELECT * FROM login  
ORDER BY FIELD(profile_type, 'Basic', 'Premium') DESC;

Upvotes: 19

Views: 46952

Answers (4)

A.A Noman
A.A Noman

Reputation: 5270

This one is more easy way to order and limit that works fine

$this->set('users', 
    $this->User->find('all', 
        array(
            'limit' => 3,
            'order' => 'User.created DESC'
       )
   )
);

Upvotes: 4

LUIS TOBIO GUTIERREZ
LUIS TOBIO GUTIERREZ

Reputation: 41

Please, try this:

$response = $this->Login->find('all', array('order'=>array('Login.profile_type'=>'desc')));

Upvotes: 2

mensch
mensch

Reputation: 4411

You can pass options to the find method:

$this->Login->find('all', array(
  'order' => "FIELD(Login.profile_type, 'Basic', 'Premium') DESC"
));

Upvotes: 4

benji
benji

Reputation: 681

Plese try this

$this->Login->find('all', array(
 'order'=>array('FIELD(Login.profile_type, "basic", "premium") DESC')
));

Upvotes: 27

Related Questions