0
votes

I could use a sanity check here ...

I have a custom property in my Cake3.0 CustomersTable object

//CustomersTable

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

I use this from a global API ajax/json controller in order to read pagination that I can set per Model

//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'],
  ]);
}

On the client side I am calling this via the Angular-based Ajax request, which has been configured to send the X-Requested-With header to trigger Cake's isAjax() handling and I'm sending requests to the .json extended Cake path to request JSON data in response.

So, when Config/app.php debug = true, everything works tickety-boo - it correctly returns the entity data. But if I toggle the debug to false, I now get ORM errors in my error log.

Error: [RuntimeException] Table "Cake\ORM\Table" is not associated with "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}

So, I have found no reason why I cannot set arbitrary properties on the Table object - indeed it works fine in debug - but why not when I flip it off?

(PS: I have purged all app/tmp cache files.)

1

1 Answers

0
votes

The paginator in 3.0 does not work the same as in 2.0. The second argument is not meant to pass options to the query. You have 2 options

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

That will set the options for the paginator to be the same as what you have stored for the table. You can alternatively use find() to hold those defaults for you:

$this->paginate($this->_table->find('all', $this->_table->paginatorDefaults));