how using SQL IN operator in find method of cakephp ORM

aya picture aya · Feb 25, 2012 · Viewed 21.4k times · Source

i am beginner in cakephp , and i want use SQL IN operator in find method , i have words table.
my code is :

$this->Word->find('Word.wordid in (83,82)');

, and this code create this query :

SELECT `Userword`.`userwordid`, `Userword`.`userid`, `Userword`.`wordid`, 
`Userword`.`date`, `Userword`.`levelid` FROM `userwords` AS `Userword` WHERE 
`Userword`.`wordid` = (82) 

but i need this query

SELECT `Userword`.`userwordid`, `Userword`.`userid`, `Userword`.`wordid`, 
Userword`.`date`, `Userword`.`levelid` FROM `userwords` AS `Userword` WHERE 
`Userword`.`wordid` IN (83,82)

how can getting like this query (using IN operator )
thanks.

Answer

mark picture mark · Feb 25, 2012

you need to let cake take care of that - simply use it as it was a string (but make sure it is an array):

$arrayOfIds = [1, 5, ...];
$this->Word->find('all', array(
    'conditions' => array('Word.wordid' => $arrayOfIds)
));