【问题标题】:cakephp: Paginate after doing 2 queriescakephp:执行 2 次查询后分页
【发布时间】:2014-03-01 18:26:34
【问题描述】:

2次查询后​​如何分页?

我有:

   $this->Paginator->settings = array(
                'conditions' => array('Record.status =' => 'published',  
                                              'RecordUser.flagged =' => null, 
                              'Record.category_id =' => $likes
                                ),
                'limit' => 5,
                'order' => array('rate' => 'desc')
            );

我也想这样做:

$this->Paginator->settings = array(
                'conditions' => array('Record.status =' => 'published',  
                                              'RecordUser.flagged =' => null, 
                              'Record.category_id !=' => $likes
                                ),
                'limit' => 5,
                'order' => array('rate' => 'desc')
            );

然后合并结果.. 问题是第一次查询的结果限制为 5,然后继续到第二页。我希望第一个查询的所有结果都显示在第二个查询的结果之前。

我该怎么做? 我正在使用 cakephp 2.4.3

【问题讨论】:

    标签: php cakephp pagination paginator


    【解决方案1】:

    为什么你不能只做一个查询并使用 order by?

    $likes = implode(',', $likes);
    
    $this->YourModel->virtualFields['in_like'] = "IF(Record.category_id IN ($likes), 0, 1)";
    
    $this->Paginator->settings = array(
        'conditions' => array(
            'Record.status =' => 'published',  
            'RecordUser.flagged =' => null, 
         ),
         'limit' => 10,
         'order' => array('in_like' => 'asc', 'rate' => 'desc')
    );
    

    【讨论】:

    • 我需要 $likes 中的那些在其他记录(第二个查询)出现之前显示在顶部
    • 所以请解释一下你的 $like 数组怎么样
    • $likes 可以包含 Category 表中的值 (category_id),也可以包含字符串(其他 $likes 不在 Category 表中)
    • 那么 category_id 怎么可能是一个字符串呢?它必须是一个ID。您必须从不是 id 的字符串中清除数组。
    • 想通了。字符串需要单引号。我怎么能忘记呢?哈哈。感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2010-12-22
    • 2016-01-05
    • 2015-12-01
    相关资源
    最近更新 更多