Ordering Doctrine Collection based on associated Entity when it is not possible to use the @orderBy annotation

前端 未结 1 1080
醉梦人生
醉梦人生 2020-12-05 10:54

I would like to understand the best way to order a Doctrine Collection based on associated Entity. In this case, it is not possible to use the @orderBy annotation.

I

相关标签:
1条回答
  • 2020-12-05 11:37

    Premise

    You proposed 5 valid/decent solutions, but I think that all could be reduced down to two cases, with some minor variants.

    We know that sorting is always O(NlogN), so all solution have theoretically the same performance. But since this is Doctrine, the number of SQL queries and the Hydration methods (i.e. converting data from array to object instance) are the bottlenecks.

    So you need to choose the "best method", depending on when you need the entities to be loaded and what you'll do with them.

    These are my "best solutions", and in a general case I prefer my solution A)

    A) DQL in a loader/repository service

    Similar to

    None of your case (somehow with 5, see the final notes note). Alberto Fernández pointed you in the right direction in a comment.

    Best when

    DQL is (potentially) the fastest method, since delegate sorting to DBMS which is highly optimized for this. DQL also gives total controls on which entities to fetch in a single query and the hydrations mode.

    Drawbacks

    It is not possible (AFAIK) to modify query generated by Doctrine Proxy classes by configuration, so your application need to use a Repository and call the proper method every time you load your entities (or override the default one).

    Example

    class MainEntityRepository extends EntityRepository
    {
        public function findSorted(array $conditions)
        {
            $qb = $this->createQueryBuilder('e')
                ->innerJoin('e.association', 'a')
                ->orderBy('a.value')
            ;
            // if you always/frequently read 'a' entities uncomment this to load EAGER-ly
            // $qb->select('e', 'a');
    
            // If you just need data for display (e.g. in Twig only)
            // return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
    
            return $qb->getQuery()->getResult();
        }
    }
    

    B) Eager loading, and sorting in PHP

    Similar to case

    Case 2), 3) and 4) are just the same thing done in different place. My version is a general case which apply whenever the entities are fetched. If you have to choose one of these, then I think that solution 3) is the most convenient, since don't mess with the entity and is always available, but use EAGER loading (read on).

    Best when

    If the the associated entities are always read, but it is not possible (or convenient) to add a service, then all entities should loaded EAGER-ly. Sorting then can be done by PHP, whenever it makes sense for the application: in an event listener, in a controller, in a twig template... If the entities should be always loaded, then an event listener is the best option.

    Drawbacks

    Less flexible than DQL, and sorting in PHP may be a slow operation when the collection is big. Also, the entities need to be hydrated as Object which is slow, and is overkill if the collection is not used for other purpose. Beware of lazy-loading, since this will trigger one query for every entity.

    Example

    MainEntity.orm.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <doctrine-mapping>
      <entity name="MainEntity">
        <id name="id" type="integer" />
        <one-to-many field="collection" target-entity="LinkedEntity" fetch="EAGER" />
        <entity-listeners>
          <entity-listener class="MainEntityListener"/>
        </entity-listeners>
      </entity>
    </doctrine-mapping>
    

    MainEntity.php:

    class MainEntityListener
    {
        private $id;
    
        private $collection;
    
        public function __construct()
        {
            $this->collection = new ArrayCollection();
        }
    
        // this works only with Doctrine 2.5+, in previous version association where not loaded on event
        public function postLoad(array $conditions)
        {
            /*
             * From your example 1)
             * Remember that $this->collection is an ArryCollection when constructor is called,
             * but a PersistentCollection when are loaded from DB. Don't recreate the instance!
             */
    
            // Get the values for the ArrayCollection and sort it using the function
            $values = $this->collection->getValues();
    
            // sort as you like
            asort($values);
    
            // Clear the current collection values and reintroduce in new order.
            $collection->clear();
            foreach ($values as $key => $item) {
                $collection->set($key, $item);
            }
        }
    }
    

    Final Notes

    • I won't use case 1) as is, since is very complicated and introduce inheritance which reduce encapsulation. Also, I think that it has the same complexity and performance of my example.
    • Case 5) is not necessarily bad. If "the service" is the application repository, and it use DQL to sort, then is my first best case. If is a custom service only to sort a collection, then I think is definitely not a good solution.
    • All the codes I wrote here is not ready for "copy-paste", since my objective was to show my point of view. Hope it would be a good starting point.

    Disclaimer

    These are "my" best solutions, as I do it in my works. Hope will help you and others.

    0 讨论(0)
提交回复
热议问题