控制器代码:控制器代码适用于雇主分页,但无法对 Stories 控制器进行分页。 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')); }模型代码:模型描述对于所有模型来说都是一样的,但是理解模型定义不能用于故事模型,但是随着我们对雇主表的模型定义的进展,它工作得很好。<?php// src/Model/Table/EmployersTable.phpnamespace 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.phpnamespace App\Model\Entity;use Cake\ORM\Entity;class Spk extends Entity{ protected $_accessible = [ '*' => true, 'id' => false, 'slug' => false, ];}<?php// src/Model/Table/StoriesTable.phpnamespace 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.phpnamespace 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
2 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
当然有可能,该功能已明确记录在案。该错误与分页无关,它只是意味着您尝试访问的属性 ( $this->Stories) 不存在。
控制器只有一个自动加载的默认模型,即根据约定与控制器名称匹配的模型,因此在您EmployersController的Employers模型中。额外的模型需要手动加载:
$this->loadModel('Stories');
// ...
$stories = $this->paginate($this->Stories, ['scope' => 'story']);
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消