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)
));
Share:
21,575
aya
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, 2020

Comments

  • aya
    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
    mark over 9 years
    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
  • urfusion
    urfusion over 8 years
    not working it build query like SELECT User.email, User.id` FROM users AS User WHERE User.specialization_id = '26,19'`
  • urfusion
    urfusion over 8 years
    $allUsers = $this->Query->User->find('all', array( 'fields' => array('User.email', 'User.email'), 'conditions' => array('User.specialization_id' => $specializationId) ));
  • mark
    mark over 8 years
    You didnt use an array!