cakephp Paginator -> sort - model option

2019-07-20 17:43发布

问题:

I am having some issues sorting the table data by a field of a different model.

From here: http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html

It says I can add in the 'model' option but when I try:

echo $this->Paginator->sort('unit', 'Unit', array('model' => 'Unit'));

I get this error:

Warning (2): array_filter() expects parameter 1 to be array, null given [CORE/Cake/View/Helper/PaginatorHelper.php, line 395]

Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE/Cake/View/Helper/PaginatorHelper.php, line 395]

Any idea what is going on here? The Main / default model is Card and I need to order by the Unit model for one of the column headings.

Thanks

回答1:

If you are showing records in a list out of some tables, then you can use it via:

<?php echo $this->Paginator->sort('Unit.unit', 'Unit');

It will perfectly work without passing third argument model option.



回答2:

Please try below code

echo $this->Paginator->sort('Unit.unit', 'Unit', array('model' => 'Unit'));

Let me know if any.



回答3:

Just a reminder for newer Versions: Associated models are not automatically loaded in CakePHP3s Paginator. Make sure you include the 'sortWhitelist' Option, see https://book.cakephp.org/3.0/en/controllers/components/pagination.html#control-which-fields-used-for-ordering