0

Below is my code and I want to pull the data based on the sequence 3, 10 then 7, how can I do that? so far it pulls first 10, then 7, then 3.

        $cars = $this->car->find('all', array(
        'conditions' => array(
            'car.id' => array(3, 10, 7)
        ),
        'limit' => 3, 
        'order' => array('car.id' => 'desc')
    ));
Dani
  • 73
  • 7
  • 1
    Possible duplicate of [MySQL specify arbitrary order by id](http://stackoverflow.com/questions/4327159/mysql-specify-arbitrary-order-by-id) – arilia Apr 15 '16 at 06:19

1 Answers1

0

Where do the conditions come from? Anyway i believe this will work:

    $cars = $this->car->find('all', array(
    'conditions' => array(
        'car.id' => array(3, 10, 7)
    ),
    'limit' => 3, 
    'order' => array('FIELD(car.id,3,10,7)')
));

Might need to play around with the cakePHP specific syntax, but the ORDER BY FIELD(fieldname, value, value value); should work

Jason Joslin
  • 1,154
  • 8
  • 23
  • That's my problem, I need the right syntex, I am still receiving errors as Call to undefined function FIELD() or syntax error, unexpected 'car' (T_STRING), expecting ')' – Dani Apr 16 '16 at 05:29
  • @Dani yeah sorry should be `'FIELD(car.id,3,10,7)'`. no quotes around car.id – Jason Joslin Apr 17 '16 at 01:16
  • Also its not specific to the question you asked. Normal cake convention would be to capitalize the first letter of the model name. I think `car.id` should be `Car.id` and `$this->Car->find(...`. would want to name the model class as `class Car extends AppModel` – Jason Joslin Apr 17 '16 at 01:26