KnpLabs / KnpPaginatorBundle

SEO friendly Symfony paginator to sort and paginate
http://knplabs.com/en/blog/knp-paginator-reborn
MIT License
1.76k stars 344 forks source link

Need a way to alias sortable fields... #196

Closed caponica closed 4 years ago

caponica commented 11 years ago

...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?

caponica commented 11 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.

Venzon commented 11 years ago

you can hide table field names using sql alias

caponica commented 11 years ago

@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.

jrmyio commented 10 years ago

Also looking for something like this.

Venzon commented 10 years ago

@ConneXNL $qb->addSelect('status as HIDDEN sort_status');

webdevilopers commented 8 years ago

:+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.

caponica commented 8 years ago

:+1:

garak commented 5 years ago

Now (since we don't rely on GET parameters anymore) you can use route parameters.

whynotOFC commented 4 years ago

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.

garak commented 4 years ago

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