Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP how to get multiple rows by array of ID's

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

like image 432
mgPePe Avatar asked Jul 25 '11 17:07

mgPePe


2 Answers

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]
    ]
]);
like image 186
Joe C Avatar answered Oct 14 '22 15:10

Joe C


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

$this->YourModel->find('all', array(
    'conditions' => array(
        "YourModel.id" => array(1, 2, 3, 4)
    )
));
like image 24
vstm Avatar answered Oct 14 '22 16:10

vstm