2016-08-03 44 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是您的數據庫表的一個例子。

相關問題