19

To find id whose value is equal to the id of an array of ids:

$this->YourModel->find('all', array(
    'conditions' => array(
        "YourModel.id" => array(1, 2, 3, 4)
    )
));

How I can do the opposite, find elements where the ids are different than an array of ids?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Martin
  • 1,282
  • 1
  • 15
  • 43

1 Answers1

35

This should work:

$this->YourModel->find('all', array(
    'conditions' => array(
        "NOT" => array( "YourModel.id" => array(1, 2, 3, 4) )
    )
));

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

BigBagel
  • 860
  • 7
  • 13
  • I'm using CakePHP 2.10 and this results in a DB error: SELECT `Division`.`division_id`, `Division`.`id` FROM `divisions` AS `Division` LEFT JOIN `division_types` AS `DivisionType` ON (`Division`.`division_type_id` = `DivisionType`.`id`) WHERE NOT (`Division`.`id` IN (1, 142, 143, 144, 168, 178, 18.... – Adam Friedman Jul 04 '20 at 00:26