CakePHP 3: Paginating model multiple times in the same view

Gepostet von Heli am 29. Jun 2018


CakePHP allows to paginate multiple models in a single controller action, using the scope option – but if you want to paginate the same model multiple times, this is not supported.

The solution: generate different models (on the fly) for using it to paginate:

In the controller:

$this->loadModel('Members');
$paginator1 = $this->paginate(
        $this->Members->find()->where(['tag' => 1]), [
    'model' => 'Members',
    'scope' => 'scope1'
        ]
);

eval('namespace App\Model\Table;'
        . 'class Members2 extends \App\Model\Table\Members {}');
$this->loadModel('Members2');
$paginator2 = $this->paginate(
        $this->Members2->find()->where(['tag' => 2]), [
    'model' => 'Members2',
    'scope' => 'scope2'
        ]
);

In the view:

// first paginator
echo $this->Paginator->sort('title', ['model' => 'Members']);
// second paginator
echo $this->Paginator->sort('title', ['model' => 'Members2']);

Kommentar absenden

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.