kh.tab
kh.tab

Reputation: 1304

Add condition to Paginate in CakePHP3

How can i add a condition to paginate in Cakephp3..

This is my code :

 $this->paginate = [
        'contain' => ['Users']
        ];
        $proprietes = $this->paginate($this->Proprietes);

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

For example Something like this: $this->paginate($this->Proprietes)->where(['status'=>'pending']);

Thank you for help.

Upvotes: 3

Views: 10577

Answers (3)

Mehdi
Mehdi

Reputation: 1477

From the Bookmarker tutorial on the official website .

public function index()
{
    $this->paginate = [
        'conditions' => [
            'Bookmarks.user_id' => $this->Auth->user('id')
             /* for a LIKE clause equivalent use :
             'Bookmarks.title LIKE ' => '%abc%' */
        ]
    ];
    $this->set('bookmarks', $this->paginate($this->Bookmarks));
    $this->set('_serialize', ['bookmarks']);
}

the documentation does not say too much about conditions in Pagination. Here is the link of the official documentation.

Upvotes: 6

Antu
Antu

Reputation: 2303

I solved this problem like this. Hope It will be work

public function viewProductList()
    {
        $shopProduct = $this->ShopProducts->find()->where(['deleted' => 0])->order(['products_id' => 'DESC']);
        $this->paginate = array(
            'limit' => 5,
        );
        $this->set('shopProduct', $this->paginate($shopProduct));

    }

Upvotes: 0

floriank
floriank

Reputation: 25698

Reading the documentation helps. Read the whole page. It even contains example code:

public function index()
{
    $query = $this->Articles->find('popular')->where(['author_id' => 1]);
    $this->set('articles', $this->paginate($query));
}

Upvotes: 9

Related Questions