0

我可以在这里使用健全性检查...

我的 Cake3.0 CustomersTable 对象中有一个自定义属性

//客户表

public $paginatorDefaults = [
    'conditions' => [...],
    'contain' => [...]
    ... etc
];

我从全局 API ajax/json 控制器中使用它来读取我可以为每个模型设置的分页

//ApiController -

//Load the components
public $components = [
    'RequestHandler', 
    'Paginator'
];


//Initialize the dynamic table object
public function initialize() {
  parent::initialize();
  //Set tableName
  $this->_tableName = $this->findTable(Inflector::camelize($this->request->model));
  //Instantiate table
  $this->_table = $this->loadModel($this->_tableName);
}

public function index() {
  $pagination = $this->_pagination(); //Loads the 
  $entities = $this->Paginator->paginate($this->_table->find(), $this->_table->paginatorDefaults);
  $this->set([
    'data' => $entities,
    'request' => $this->request,
    '_serialize' => ['data','request'],
  ]);
}

在客户端,我通过基于 Angular 的 Ajax 请求调用它,该请求已配置为发送 X-Requested-With 标头以触发 Cake 的 isAjax() 处理,并且我将请求发送到 .json 扩展 Cake 路径到请求 JSON 数据作为响应。

因此,当 Config/app.php debug = true 时,一切正常,tickety-boo - 它正确返回实体数据。但是,如果我将调试切换为 false,我现在会在错误日志中看到 ORM 错误。

错误:[RuntimeException] 表“Cake\ORM\Table”未与“paginatorDefaults”关联

2014-10-24 16:49:44 Error: [RuntimeException] Table "Cake\ORM\Table" is not associated with "paginatorDefaults"
Request URL: /Tremendus/Momento/api/customers.json?sort=Customers.name&direction=asc&limit=10&page=1
Stack Trace:
0 /<masked path>/plugins/Api/src/Controller/ApiController.php(92): Cake\ORM\Table->__get('paginatorDefaul...')
1 /<masked path>/plugins/Api/src/Controller/ApiController.php(78): Api\Controller\ApiController->_pagination()
2 [internal function]: Api\Controller\ApiController->index()
3 /<masked path>/vendor/cakephp/cakephp/src/Controller/Controller.php(411): call_user_func_array(Array, Array)
4 /<masked path>/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(111): Cake\Controller\Controller->invokeAction()
5 /<masked path>/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(85): Cake\Routing\Dispatcher->_invoke(Object(Api\Controller\ApiController))
6 /<masked path>/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
7 {main}

所以,我没有发现为什么我不能在 Table 对象上设置任意属性 - 实际上它在调试中工作正常 - 但是当我关闭它时为什么不呢?

(PS:我已经清除了所有的 app/tmp 缓存文件。)

4

1 回答 1

0

3.0 中的分页器与 2.0 中的不同。第二个参数并不意味着将选项传递给查询。你有2个选择

$this->paginate = $this->_table->paginatorDefaults

这会将分页器的选项设置为与您为表存储的选项相同。您也可以使用 find() 为您保留这些默认值:

$this->paginate($this->_table->find('all', $this->_table->paginatorDefaults));
于 2014-10-28T14:52:51.447 回答