sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Sorting of list defined in overwritten AbstractAdmin::createQuery() method does not work #5599

Closed kniziol closed 2 years ago

kniziol commented 5 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'

sonata-project/admin-bundle              3.49.1 3.49.1 The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.0 3.17.0 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.3.0  1.3.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.9.0  3.9.0  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library

Symfony packages

$ composer show --latest 'symfony/*'

symfony/asset                      v4.3.1  v4.3.1  Symfony Asset Component
symfony/browser-kit                v4.3.1  v4.3.1  Symfony BrowserKit Component
symfony/cache                      v4.3.1  v4.3.1  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v1.1.5  v1.1.5  Generic abstractions related to caching
symfony/class-loader               v3.4.28 v3.4.28 Symfony ClassLoader Component
symfony/config                     v4.3.1  v4.3.1  Symfony Config Component
symfony/console                    v4.3.1  v4.3.1  Symfony Console Component
symfony/contracts                  v1.1.0  v1.1.5  A set of abstractions extracted out of the Symfony components
symfony/css-selector               v4.3.1  v4.3.1  Symfony CssSelector Component
symfony/debug                      v4.3.1  v4.3.1  Symfony Debug Component
symfony/debug-bundle               v4.3.1  v4.3.1  Symfony DebugBundle
symfony/debug-pack                 v1.0.7  v1.0.7  A debug pack for Symfony projects
symfony/dependency-injection       v4.3.1  v4.3.1  Symfony DependencyInjection Component
symfony/doctrine-bridge            v4.3.1  v4.3.1  Symfony Doctrine Bridge
symfony/dom-crawler                v4.3.1  v4.3.1  Symfony DomCrawler Component
symfony/dotenv                     v4.3.1  v4.3.1  Registers environment variables from a .env file
symfony/event-dispatcher           v4.3.1  v4.3.1  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.5  v1.1.5  Generic abstractions related to dispatching event
symfony/expression-language        v4.3.1  v4.3.1  Symfony ExpressionLanguage Component
symfony/filesystem                 v4.3.1  v4.3.1  Symfony Filesystem Component
symfony/finder                     v4.3.1  v4.3.1  Symfony Finder Component
symfony/flex                       v1.2.7  v1.2.7  Composer plugin for Symfony
symfony/form                       v4.3.1  v4.3.1  Symfony Form Component
symfony/framework-bundle           v4.3.1  v4.3.1  Symfony FrameworkBundle
symfony/http-client                v4.3.1  v4.3.1  Symfony HttpClient component
symfony/http-client-contracts      v1.1.5  v1.1.5  Generic abstractions related to HTTP clients
symfony/http-foundation            v4.3.1  v4.3.1  Symfony HttpFoundation Component
symfony/http-kernel                v4.3.1  v4.3.1  Symfony HttpKernel Component
symfony/inflector                  v4.3.1  v4.3.1  Symfony Inflector Component
symfony/intl                       v4.3.1  v4.3.1  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle               v1.11.6 v1.11.6 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                       v4.3.1  v4.3.1  A library to manipulate MIME messages
symfony/monolog-bridge             v4.3.1  v4.3.1  Symfony Monolog Bridge
symfony/monolog-bundle             v3.3.1  v3.4.0  Symfony MonologBundle
symfony/options-resolver           v4.3.1  v4.3.1  Symfony OptionsResolver Component
symfony/orm-pack                   v1.0.6  v1.0.6  A pack for the Doctrine ORM
symfony/panther                    v0.4.1  v0.4.1  A browser testing and web scraping library for PHP and Symfony.
symfony/phpunit-bridge             v4.2.9  v4.3.1  Symfony PHPUnit Bridge
symfony/polyfill-intl-icu          v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.11.0 v1.11.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring          v1.11.0 v1.11.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.11.0 v1.11.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/process                    v4.3.1  v4.3.1  Symfony Process Component
symfony/profiler-pack              v1.0.4  v1.0.4  A pack for the Symfony web profiler
symfony/property-access            v4.3.1  v4.3.1  Symfony PropertyAccess Component
symfony/property-info              v4.3.1  v4.3.1  Symfony Property Info Component
symfony/proxy-manager-bridge       v4.3.1  v4.3.1  Symfony ProxyManager Bridge
symfony/routing                    v4.3.1  v4.3.1  Symfony Routing Component
symfony/security-acl               v3.0.2  v3.0.2  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.3.1  v4.3.1  Symfony SecurityBundle
symfony/security-core              v4.3.1  v4.3.1  Symfony Security Component - Core Library
symfony/security-csrf              v4.3.1  v4.3.1  Symfony Security Component - CSRF Library
symfony/security-guard             v4.3.1  v4.3.1  Symfony Security Component - Guard
symfony/security-http              v4.3.1  v4.3.1  Symfony Security Component - HTTP Integration
symfony/serializer                 v4.3.1  v4.3.1  Symfony Serializer Component
symfony/serializer-pack            v1.0.2  v1.0.2  A pack for the Symfony serializer
symfony/service-contracts          v1.1.5  v1.1.5  Generic abstractions related to writing services
symfony/stopwatch                  v4.3.1  v4.3.1  Symfony Stopwatch Component
symfony/swiftmailer-bundle         v3.2.8  v3.2.8  Symfony SwiftmailerBundle
symfony/templating                 v4.3.1  v4.3.1  Symfony Templating Component
symfony/test-pack                  v1.0.5  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.3.1  v4.3.1  Symfony Translation Component
symfony/translation-contracts      v1.1.5  v1.1.5  Generic abstractions related to translation
symfony/twig-bridge                v4.3.1  v4.3.1  Symfony Twig Bridge
symfony/twig-bundle                v4.3.1  v4.3.1  Symfony TwigBundle
symfony/validator                  v4.3.1  v4.3.1  Symfony Validator Component
symfony/var-dumper                 v4.3.1  v4.3.1  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v4.3.1  v4.3.1  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                   v4.3.1  v4.3.1  Symfony WebLink Component
symfony/web-profiler-bundle        v4.3.1  v4.3.1  Symfony WebProfilerBundle
symfony/web-server-bundle          v4.3.1  v4.3.1  Symfony WebServerBundle
symfony/webpack-encore-bundle      v1.6.0  v1.6.0  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       v4.3.1  v4.3.1  Symfony Yaml Component

PHP version

$ php -v

PHP 7.3.6 (cli) (built: May 31 2019 23:33:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.6, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans

Subject

There is a bug when custom sorting of list is defined in overwritten \Sonata\AdminBundle\Admin\AbstractAdmin::createQuery() method. It does not work.

Steps to reproduce

Overwrite createQuery() method in your *Admin class and call the setSortOrder() and setSortBy() methods on the Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery instance to define custom sorting:

final class NewsAdmin extends AbstractAdmin
{
    // (...)

    public function createQuery($context = 'list'): ProxyQuery
    {
        /** @var ProxyQuery $query */
        $query = parent::createQuery($context);

        return $query
            ->setSortOrder('DESC')
            ->setSortBy([], ['fieldName' => 'title'])
            ;
    }

    // (...)
}

Expected results

List is sorted by field and with direction defined in overwritten method - title and DESC.

Actual results

List is sorted by default field and with default direction - id and ASC.

phansys commented 5 years ago

Hi @kniziol! Please, take a look at https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/action_list.html#configure-the-default-ordering-in-the-list-view.

The allowed way to define the sort of an admin list is by configuring AbstractAdmin::$datagridValues. I see your changes at #5598, and I think this property must keep their current behavior, overriding any previous configured sort in the query.

IMO, you should allow to override the default sorting by "id" if the custom query defines another field, but keeping the chance to override that default sorting with the configuration provided at $datagridValues['_sort_by'] and $datagridValues['_sort_order'].

kniziol commented 5 years ago

Hi @phansys 🙃

Please, take a look at https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/action_list.html#configure-the-default-ordering-in-the-list-view.

I understand and it works fine, but if you want to do it, let's say, more programmatically, overwritten method is the correct place, I think.

IMO, you should allow to override the default sorting by "id" if the custom query defines another field, but keeping the chance to override that default sorting with the configuration provided at $datagridValues['_sort_by'] and $datagridValues['_sort_order'].

So, the sequence should looks like this?

  1. if $datagridValues['_sort_by'] and $datagridValues['_sort_order'] are same as default and $this->query->getSortBy() and $this->query->getSortOrder() are not equal to null, use $this->query->getSortBy() and $this->query->getSortOrder()
  2. otherwise, use $datagridValues['_sort_by'] and $datagridValues['_sort_order']
phansys commented 5 years ago

I'd say, the configuration must respect the following order (the higher order has more priority):

  1. Use the default model identifier;
  2. Use the sort configured at the query;
  3. Use what is configured in $datagridValues['_sort_by'] and $datagridValues['_sort_order'].
kniziol commented 5 years ago

I've made modifications as you proposed. Take a look at 13e3308 commit.

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

VincentLanglet commented 4 years ago

I added the keep label since there is a WIP PR for this issue https://github.com/sonata-project/SonataAdminBundle/pull/5598

VincentLanglet commented 2 years ago

Closing this since now there is a configureDefaultSortValues method which allow to configure the sort dynamically.