Sorting on Associated Models with the CakePHP3 Paginator Component

I was a bit stumped recently while attempting to sort on associated models using Cake 3’s paginator component. It would only sort on main models fields. After finally reading through ALL of the documentation I stumbled upon the “sortWhiteList” option. Cake makes you use this in order to sort on associated models. Say we have a Cities model, which has associations to a Counties and States model. To sort on the and field we would use the following configuration:

Now inside the PaginatorHelper these can be referenced as follows:



  • Rohan pande says:

    Hi Chris
    Thank you for this blog it helped me a lot
    However I was wondering can I do sorting on the fields which does not exists in primary or associated table like a count of comments per post.

  • Tsumannai says:

    Hello, thanks for the article.

    May I ask, what will you do when you have deeper level associated models?

    But wait, does your Countries “hasMany” both States and Cities?

    What if Countries “hasMany” only States, States “hasMany” Cities? This is what I meant deeper level. As in some cases I don’t want Countries “hasMany” Cities.

    Thank you!