11

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.

aya
  • 1,597
  • 4
  • 29
  • 59

1 Answers1

28

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)
));
mark
  • 21,691
  • 3
  • 49
  • 71
  • 19
    Note that this is only valid for CakePHP <= 2.x. In 3.x you will need to manually add IN again: `'Word.wordid IN' => $arrayOfIds` – mark Sep 21 '14 at 22:16
  • not working it build query like `SELECT `User`.`email`, `User`.`id` FROM `users` AS `User` WHERE `User`.`specialization_id` = '26,19'` – urfusion Sep 17 '15 at 19:09
  • `$allUsers = $this->Query->User->find('all', array( 'fields' => array('User.email', 'User.email'), 'conditions' => array('User.specialization_id' => $specializationId) ));` – urfusion Sep 17 '15 at 19:10
  • You didnt use an array! – mark Sep 18 '15 at 11:32