Search code examples
sortinginternationalizationcakephp-3.0paginator

Cakephp 3 paginator sort fields with translate behavior i18n


I have a Posts table with Translate behavior and I want to sort it by title on the table at index view. I can't sort by the fields that are translated because the field's don't actually exist on the database. They exist on the i18n table.

This, as I explained above, doesn't work:

$this->Paginator->sort('title');
$this->Paginator->sort('Posts_title_translation.content');

So, what I should do? What am I missing?

Many Thanks!


Solution

  • The second variant should work once the field is being whitelisted, which is generally necessary for associations to be used for sorting:

    $this->paginate = [
        // ...
        'sortWhitelist' => [
            'Posts_title_translation.content',
            // ...
        ]
    ];
    

    Note that with the whitelist in place, you will have to add all other fields of your Posts table that need to be used for sorting too!

    See also Cookbook > ... Components > Pagination > Control which Fields Used for Ordering