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

[Question/discussion] createQuery() purpose #5024

Closed pribeirojtm closed 4 years ago

pribeirojtm commented 6 years ago

Environment

Sonata packages

sonata-project/admin-bundle              3.33.0 3.33.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.12.1 3.12.1 Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  1.0.7  Cache library
sonata-project/cache-bundle              2.4.2  2.4.2  This bundle provides caching services
sonata-project/core-bundle               3.9.1  3.9.1  Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.3.1  2.3.1  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2  1.0.2  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.4.2  3.4.2  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.8.0  1.8.0  Lightweight Exporter library

Symfony packages

symfony/assetic-bundle     v2.8.2 v2.8.2 Integrates Assetic into Symfony2
symfony/monolog-bundle     v3.2.0 v3.2.0 Symfony MonologBundle
symfony/phpunit-bridge     v4.0.6 v4.0.6 Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.7.0 v1.7.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-iconv     v1.7.0 v1.7.0 Symfony polyfill for the Iconv extension
symfony/polyfill-intl-icu  v1.7.0 v1.7.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.7.0 v1.7.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.7.0 v1.7.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.7.0 v1.7.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.7.0 v1.7.0 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.1 v3.0.1 Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v3.2.1 v3.2.1 Symfony SwiftmailerBundle
symfony/symfony            v3.4.6 v3.4.6 The Symfony PHP framework

PHP version

PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 14:12:46) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.27-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

Subject

This is just a question. From the docs, it seems that this admin method can be overwritten to customize the query used from list view:

Customizing the query used to generate the list

It is meant just for the list? Because I see a param with the $context, but it is deprecated since sf 3.3, and it is removed in 4.0.

Inner question: The query is intimately related with the configureListFields() method, right? We can only use the alias present in the "Select" given by the query. If we don't use createQuery, then we lazyload entities data from the association present in the entity, right?

I understand that the method exists because sometimes in the list view we are listing many objects, and related objects also, lazyloading entities for each row, making unnecessary queries to database and also consuming a lot of memory resources. That's the purpose of this method, we can customize the query to just make one query to database which populates all the necessary objects for listing. So far so good.

And for other contexts, with dont we have the same functionality? Example: Exporting information (the list information) we can have the same type of functionality, because we end in the same problem of the listing. Even for the configureFormFields, we need this kind of helper, because of the lazyloading of many related entities to the main object entity. How can I handle this?

Thanks for your time.

zhil commented 6 years ago

I am using createQuery for limiting entities, which user can see/edit/filter etc. (based on custom logic for different entities). Everything works just great, except HelperController->retrieveAutocompleteItemsAction() (used in ModelAutocompleteType) do not call createQuery.

Just wonder - am I using createQuery correctly and should I create PR with bugfix?

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

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

pribeirojtm commented 4 years ago

Hello,

I know we can use this createQuery method to for example, limiting entities appearing on the list. But I still have some questions that seems not well clarified in the docs, if someone can help in here:

I understand that the method exists because sometimes in the list view we are listing many objects, and related objects also, lazyloading entities for each row, making unnecessary queries to database and also consuming a lot of memory resources. That's the purpose of this method, we can customize the query to just make one query to database which populates all the necessary objects for listing. So far so good. This is my understanding of the purpose of this method, to give that possibility.

But what about other contexts? Do we have the same functionality?

Can we improve somehow the docs regarding these explanations?

Thanks in advance.

VincentLanglet commented 4 years ago

Since we're dropping the context param in the next major, there won't be some documentation update about this. I recommend you to not rely on this context param.

If you want to filter the export, just uses some filters before clicking on the export button.