2016-08-03 20 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。あなたのコントローラではなく、[**ヘルパー**](http://book.cakephp.org/3.0/ja/views/helpers.html)ではなく、ビューテンプレート。前述したように、 'param()'メソッドはページビューアーのビューヘルパーに属しています。つまり、自分のビューにデータを渡す必要はありません。 – 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は、あなたのDBテーブルの一例です。

関連する問題