【问题标题】:CakePHP: how to get total number of records retrieved with paginationCakePHP:如何获取分页检索的记录总数
【发布时间】:2018-04-12 21:04:09
【问题描述】:

当您使用$this->paginate('Modelname') 和某些页面limit 检索记录时,您如何获得检索到的记录总数?

我想在视图上显示这个总数,但count($recordsRetrieved) 返回仅在当前页面上显示的数字。因此,如果检索到的记录总数为 99 且 limit 设置为 10,则返回 10,而不是 99。

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    你可以debug($this->Paginator->params());

    这会给你

    /*
    Array
    (
        [page] => 2
        [current] => 2
        [count] => 43
        [prevPage] => 1
        [nextPage] => 3
        [pageCount] => 3
        [order] =>
        [limit] => 20
        [options] => Array
            (
                [page] => 2
                [conditions] => Array
                    (
                    )
            )
        [paramType] => named
    )
    */
    

    PHP的最终代码>=5.4:

    $this->Paginator->params()['count'];

    对于低于 5.4 的 PHP 版本:

    $paginatorInformation = $this->Paginator->params();
    $totalPageCount = $paginatorInformation['count'];
    

    【讨论】:

      【解决方案2】:

      要获得答案,请访问以下链接 http://book.cakephp.org/2.0/en/controllers/request-response.html

      使用pr($this->request->params)你会发现所有分页的东西

      【讨论】:

        【解决方案3】:

        对于蛋糕 3.x

        你可以使用getPagingParams方法

        示例:

        debug($this->Paginator->getPagingParams());
        

        输出:

        [
            'users' => [
                'finder' => 'all',
                'page' => (int) 1,
                'current' => (int) 5,
                'count' => (int) 5,
                'perPage' => (int) 1,
                'prevPage' => false,
                'nextPage' => true,
                'pageCount' => (int) 5,
                'sort' => null,
                'direction' => false,
                'limit' => null,
                'sortDefault' => false,
                'directionDefault' => false,
                'scope' => null
            ]
        ]
        

        【讨论】:

          【解决方案4】:

          这是我从控制器获得计数的方法*

          这是针对 CakePHP 2.3 的。它使用了一个视图助手,这在控制器中通常是一个禁忌,因为它违反了 MVC,但在这种情况下,我认为保持代码 DRY 是有意义的。

          // Top of file
          App::uses('PaginatorHelper', 'View/Helper');
          
          // Later in controller method
          $paginatorHelper = new PaginatorHelper(new View(null));
          $records = $this->paginate();
          $count = $paginatorHelper->params()['count'];
          

          * 我知道 OP 从视图中询问过,但我认为 Arzon Barua 的回答是否对人们有所帮助(尽管我认为它只告诉您请求的计数,而不是 OP 想要的实际计数),那么这可能会有所帮助也是。

          【讨论】:

            【解决方案5】:

            这种方式是通过控制器获取分页信息。

            class YourController extends Controller{
              $helpers = array('Paginator');
              public fn(){
                $view = new View(null);
                var_dump($view->paginator->params());
              }
            }
            

            【讨论】:

              猜你喜欢
              • 2016-12-09
              • 1970-01-01
              • 2020-03-05
              • 1970-01-01
              • 2014-07-16
              • 2017-04-04
              • 2013-12-19
              • 1970-01-01
              相关资源
              最近更新 更多