我可以在这里使用健全性检查......
我的Cake3.0 CustomersTable对象中有自定义属性
// 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时,一切都工作正常 - 它正确返回实体数据。但是,如果我将调试切换为false,我现在会在错误日志中收到ORM错误。
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缓存文件。)
答案 0 :(得分:0)
3.0中的分页器与2.0中的分页器不同。第二个参数并不是要将选项传递给查询。你有2个选择
$this->paginate = $this->_table->paginatorDefaults
这将设置paginator的选项与您为表存储的选项相同。您也可以使用find()来保存这些默认值:
$this->paginate($this->_table->find('all', $this->_table->paginatorDefaults));