9

I would like to pull from database multiple rows, according to a list of array of ID's.

In some other frameworks there seem to be something like "WHERE_IN", but not here.

Can someone tell me how to do it?

I would like to know how to do that through the find() or read() (or any other cakephp function) and NOT build a query manually, since I want all data to be escaped and secure.

thank you

1
  • Thank you for your question, i was preparing my self to code some monstrosity before i googled this :D. Commented Oct 17, 2013 at 18:39

2 Answers 2

26

According to "Complex Find Functions" (third example) this should work:

$this->YourModel->find('all', array(
    'conditions' => array(
        "YourModel.id" => array(1, 2, 3, 4)
    )
));
Sign up to request clarification or add additional context in comments.

4 Comments

to do the same but with id different of array?
@Martin: thank you for the question. I'm not sure what you mean - do you want to select records whose IDs are not in the array like the NOT IN-operator? Or do you want to select using another field than id
I already solved the doubt, thanks anyway stackoverflow.com/questions/14508682/…
On CakePHP 3 this throws InvalidArgumentException: Cannot convert value to integer
1

It looks like in the newer CakePHPs you need to specify 'IN'. This solves aexl question.

$this->YourModel->find('all', [
    'conditions' => [
        "YourModel.id IN" => [1, 2, 3, 4]
    ]
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.