2016-08-03 77 views
1

我想显示查询的记录总数。问题是我使用paginator,所以..只显示页面的记录数,我需要所有记录的数量。获取在Cakephp中分页检索到的记录总数

这是我的代码:

public function index() 
{ 
    $paisFK = $this -> Auth -> User()['paisFK']; 

    $this->paginate['contain'] = ['Ciudades']; 
    $this->paginate['conditions'] = ['Ciudades.paisFK' => $paisFK]; 

    $complejos = $this->paginate($this->Complejos); 

    $this->set(compact('complejos')); 
    $this->set('_serialize', ['complejos']); 

    //Obtenemos la cantidad de complejos: 

    $number = $complejos->count(); 
    $this->set('number',$number); 
} 

有人能帮助我吗?谢谢!

回答

2

paginator组件使用键paging为请求对象params添加分页详细信息,其中数据使用表对象的别名嵌套。

检查

debug($this->request->param('paging')); 

这个数据是通过分页程序佣工param()方法在视图模板方便。

$this->Paginator->param('count'); 

此外,这取决于你想如何使用的价值,你也可以使用counter()方法与自定义格式。它支持各种令牌,包括{{count}}

$this->Paginator->counter('There is a total of {{count}} record(s)...'); 

又见

+0

我在尝试$ number = $ this-> Paginator-> param('count'); $ this-> set('number',$ number);但有错误:“错误:调用未定义的方法Cake \ Controller \ Component \ PaginatorComponent :: param()” –

+0

@FederickJons因为您正在访问[**组件**](http://book.cakephp。 org/3.0/en/controllers/components.html)在你的控制器中,而不是[** helper **](http://book.cakephp.org/3.0/en/views/helpers.html)视图模板。如前所述,'param()'方法属于paginator视图助手,即不需要自行传递任何数据到视图,它将自动在那里可用。 – ndm

0

有一个完整的解决方案,如果你遵循这一点,我觉得这里的解决方案:

控制器:

public $paginate =[ 
    'limit' => 10, 
    'order' => [ 
     'tble.id' => 'asc' 
    ] 
]; 

public function initialize() 
{ 
    parent::initialize(); 
    $this->loadComponent('Paginator'); 
} 

public function index() 
{ 
    $product = TableRegistry::get('tble'); 
    $query = $product->find(); 
    $this->set(array('data'=>$query)); 
    $this->set('tble', $this->paginate($query)); 


    $this->render('index'); 
} 

Index.ctp:

<?= $this->Paginator->prev('<< Previous') ?> 
<?= $this->Paginator->next('Next >>') ?> 
<?= $this->Paginator->counter() ?> 

tble是您的数据库表的一个例子。