I have a “User” entity that contains a ‘first_name’ field and a ‘last_name’ field, along with a concatenated virtual field called ‘full_name.’ In my paginated display of User entities, I use the full_name field in the template. However, because this is a paginated display, and there is no data base column called ‘full_name’, Paginator isn’t able to sort the field.
How can I tell paginator to sort on the ‘first_name’ field rather than ‘full_name?’
You can set up and your controller the options to paginate on any field Look at how cake bake does it and see
Using Controller::paginate() in the docs.
I figured it out, but it’s done with the Paginator Helper in the view. The controller is always looking for a DB column to sort on and that is either configured in the controller (which I couldn’t get to work) or handed to it through the pagination link as the “sort” key which is set to the column name. Because my entity uses a virtual field, this column doesn’t exist so the controller can’t create the correct “order by” on the query.
So in my view when I’m publishing my pagination information, I use the two property call to set up the pagination link correctly.
<?= $this->Paginator->sort('first_name', 'Name') ?>
will create the link with the “first_name” value on sort, rather than the column name from the entity.
Thanks for the help!