是否有可能使分页适用于同一控制器和视图中的多个表

is there a possibility to make pagination work for multiple tables within same controller and view

控制器代码:控制器代码适用于雇主分页,但无法用于故事控制器的分页。

    public $paginate = [
        'Employers' => ['scope' => 'employer'],
        'Stories' => ['scope' => 'story']
    ];

    public function index()
    {
        // Paginate property
        $this->loadComponent('Paginator');

        // In a controller action
        $stories = $this->paginate($this->Stories, ['scope' => 'story']);
        $employers = $this->paginate($this->Employers, ['scope' => 'employer']);
        pr($stories);
        $this->set(compact('employers', 'stories'));

    }

模型代码:所有模型的模型描述都相同,但了解模型定义无法用于故事模型,但随着我们对雇主的模型定义的进展 table 绝对可以正常工作。

<?php
// src/Model/Table/EmployersTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class EmployersTable extends Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Timestamp');
    }
}

<?php
// src/Model/Entity/Employer.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Spk extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'slug' => false,
    ];
}

<?php
// src/Model/Table/StoriesTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class StoriesTable extends Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Timestamp');
    }
}

<?php

// src/Model/Entity/Story.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Sty extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'slug' => false,
    ];
}

当我完成加载页面操作时,我一直在查看错误,我遇到调用了雇主数据但无法加载故事数据。建议开放查看期待您的解答。

错误信息:

Undefined property: EmployersController::$Stories in /Applications/MAMP/htdocs/sd/sd/src/Controller/EmployersController.php

当然可以,这个特征是明确的documented。该错误与分页无关,它只是意味着您尝试访问的 属性 ($this->Stories) 不存在。

控制器只有一个自动加载的默认模型,即根据约定与控制器名称匹配的模型,因此在您的EmployersController中就是Employers模型。需要手动加载其他模型:

$this->loadModel('Stories');

// ...

$stories = $this->paginate($this->Stories, ['scope' => 'story']);

另见

不,这是不可能的,因为 CakePHP 仅适用于对同一模型有多个分页查询请求的单个 table。但不适用于许多模型。