开发者

Cakephp pagination with or condition

I want to be able to put 'or' condition inside a foreach loop when defining pagination variable at the controller like below:

foreach($p as $ps){
$this->paginate = array(
                'o开发者_JAVA技巧r' => array(
                    array('Petition.commune_id LIKE' => $commune_ids['Commune']['id'] . ","),

                    ),Recipe
                'limit' => 10
            );

}

I know above is wrong but I've compiled the codes for demonstration purposes. Is there a way we can do this in cake?

I need to loop because I want to be able to use ids from another table (which has been fetched as an array by find method) and use this for looping in the current table which is Petitions table. $commune_ids['Commune']['id'] is what I need to loop through as or value. Alternatively the query will be like:

SELECT * FROM petitions WHERE commune_id = 971 OR commune_id = 123 OR commune_id = 321 OR commune_id = 432 

but of course I can do this in find function but I would like to paginate this results...


What you want is the query:

SELECT * FROM petitions WHERE commune_id IN (971, 123, ...)

In Cake, that's:

'conditions' => array(
    'Petition.commune_id' => array(971, 123, ...)
)

So you make an array of ids which you can put into the condition. Done.

$communeIds = /* assemble array somehow */;

$this->paginate = array(
    'conditions' => array(
        'Petition.commune_id' => $communeIds
    ),
    'limit' => 10
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜