how using SQL IN operator in find method of cakephp ORM
21,575
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)
));
Author by
aya
I am web developer, and create application based on php. I like use standard code and framework. I'm beginning Cakephp and Laravel and mastered on Codeigniter.
Updated on February 22, 2020Comments
-
aya about 4 years
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. -
mark over 9 yearsNote that this is only valid for CakePHP <= 2.x. In 3.x you will need to manually add IN again:
'Word.wordid IN' => $arrayOfIds
-
urfusion over 8 yearsnot working it build query like
SELECT
User.
email,
User.
id` FROMusers
ASUser
WHEREUser
.specialization_id
= '26,19'` -
urfusion over 8 years
$allUsers = $this->Query->User->find('all', array( 'fields' => array('User.email', 'User.email'), 'conditions' => array('User.specialization_id' => $specializationId) ));
-
mark over 8 yearsYou didnt use an array!