简体   繁体   English

Cakephp分页或条件

[英]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循环中,如下所示:

foreach($p as $ps){
$this->paginate = array(
                'or' => 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. 我需要循环,因为我希望能够使用另一个表的ID(已通过find方法将其作为数组读取),并将其用于当前表(即Petitions表)中的循环。 $commune_ids['Commune']['id'] is what I need to loop through as or value. $ commune_ids ['Commune'] ['id']是我需要遍历as或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... 但是我当然可以在find函数中执行此操作,但是我想对结果进行分页...

What you want is the query: 您想要的是查询:

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

In Cake, that's: 在Cake中,这是:

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

So you make an array of ids which you can put into the condition. 因此,您创建了一个ID数组,可以将其放入条件中。 Done. 做完了

$communeIds = /* assemble array somehow */;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM