【问题标题】:Cakephp pagination with or condition带有或条件的Cakephp分页
【发布时间】:2011-08-21 22:07:01
【问题描述】:

在控制器上定义分页变量时,我希望能够将“或”条件放入 foreach 循环中,如下所示:

foreach($p as $ps){
$this->paginate = array(
                'or' => array(
                    array('Petition.commune_id LIKE' => $commune_ids['Commune']['id'] . ","),

                    ),Recipe
                'limit' => 10
            );

}

我知道上面是错误的,但我已经编译了代码用于演示目的。有没有办法在蛋糕中做到这一点?

我需要循环,因为我希望能够使用另一个表中的 id(已通过 find 方法作为数组获取)并使用它在当前表(即请愿表)中循环。 $commune_ids['Commune']['id'] 是我需要循环遍历的值。或者,查询将类似于:

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

当然我可以在 find 函数中执行此操作,但我想对结果进行分页...

【问题讨论】:

  • 对不起,这没有任何意义。为什么首先要循环?
  • 我需要循环,因为我希望能够使用另一个表中的 ids(已通过 find 方法作为数组获取)并将其用于在当前表(即请愿表)中循环。 $commune_ids['Commune']['id'] 是我需要循环遍历的值。或者,查询将类似于:SELECT * FROM petitions WHERE commune_id = 971 OR commune_id = 123 OR commune_id = 321 OR commune_id = 432 但当然我可以在 find 函数中执行此操作,但我会喜欢给这个结果分页...
  • 如果您有很多代码或解释要做,请不要只是在评论中添加它; 编辑问题,这样我们都可以从格式良好的问题中受益。

标签: php cakephp


【解决方案1】:

你想要的是查询:

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

在 Cake 中是:

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

因此,您可以创建一个可以放入条件中的 id 数组。完成。

$communeIds = /* assemble array somehow */;

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

【讨论】:

  • 我们不只是喜欢 Cake 让我们的生活更轻松吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多