5

Is there a way to do a find() in CakePHP that converts to an IN condition? It seems the find() methods just take a single value to search on.

I would like to do something like this:

$this->User->findAllById(array(1, 5, 7));

which would convert the SQL to something like:

SELECT * FROM users WHERE id IN (1, 5, 7);
tereško
  • 58,060
  • 25
  • 98
  • 150
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83

1 Answers1

13
$this->User->find('all', array('conditions' => array('id' => array(1, 5, 7))));
deceze
  • 510,633
  • 85
  • 743
  • 889
  • This works for integers but doesn't seem to work on string values. – Matt McCormick Jan 18 '12 at 08:04
  • It should work just fine for strings as well. What does the generated query look like? – deceze Jan 18 '12 at 08:06
  • Sorry, I'm new to Cake. I can't seem to find how to easily print out the generated query. I changed the ints to 'test', 'test2' and I'm getting the error: Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'test' in 'where clause' – Matt McCormick Jan 18 '12 at 08:10
  • Set debug to 2 in `config/core.php`, look at the bottom of the page. And the error indicates an error in your query, you seem to be searching for the field `test`, which does not exist. – deceze Jan 18 '12 at 08:16
  • Ah, was doing 'id', array(...) instead of 'id' => array(...) – Matt McCormick Jan 18 '12 at 08:17