Closed caponica closed 4 years ago
An alternative way to do this might be to use the sortFieldWhitelist
option.
Current usage of this is to set something like:
array('p.page_title', 'p.create_date');
If we extended this to allow you to set an alias this would be quite neat:
array(
'pt' => 'p.page_title',
'date' => 'p.create_date'
);
Is this of interest and a reasonable solution? If so let me know and I'll work up a pull request.
You'd need to refactor all the subscriber classes to enable them to work with aliases, but it should not cause any BC breaks since the array values are still the alias.field
values that are expected.
you can hide table field names using sql alias
@Venzon - not sure how to do this using KnpPaginator with Doctrine ORM? It's not easy to set reliable column aliases, which is why I suggested adding something to KnpPaginator itself.
Also looking for something like this.
@ConneXNL
$qb->addSelect('status as HIDDEN sort_status');
:+1: for this. Quite a few people ask for this:
I see several advantages from mapping aliases to columns:
I'm just introducing this bundle to our current project. And I'm pretty sure our frontend guy does NOT want to ask the backend guy what names he has tu use for sorting inside his templates.
Nice workaround by @Venzon though! Works like a charm.
:+1:
Now (since we don't rely on GET
parameters anymore) you can use route parameters.
Now (since we don't rely on
GET
parameters anymore) you can use route parameters.
I am new to Symfony and do not understand your answer. Can you perhaps explain that a little more?
It would be really helpful to know how aliases can be used for sorting.
Now (since we don't rely on
GET
parameters anymore) you can use route parameters.I am new to Symfony and do not understand your answer. Can you perhaps explain that a little more?
It would be really helpful to know how aliases can be used for sorting.
You can put the values you need for sorting in your route placeholders
...so you're not putting database fields into public URLs.
Adding a paginator method "setSortAlias" should do the trick.
Then, in your controller:
$paginator->setSortAlias('p.page_title', 'pt');
In your twig template:
{{ knp_pagination_sortable(paginatedPages, 'Page Title', 'pt') }}
And instead of:
http://example.com/mypage?sort=p.page_title&direction=desc
your URL would be:
http://example.com/mypage?sort=pt&direction=desc
Would this be a popular enhancement - or is it possible some way already?