Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1681

loadRelated() - Method to efficiently load sets of related entities in "sub"-select strategies

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

      As per Request of Seldaek

        Activity

        Benjamin Eberlei created issue -
        Hide
        Jordi Boggiano added a comment - - edited

        Sample:

        $result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult();
        $result->loadRelated('roles'); // loads all a.roles
        

        Would be the equivalent of:

        $result = $queryBuilder->select('a, r')->from('User', 'a')->join('a.roles', 'r')->getQuery()->getResult();
        

        Except that the above does one simple query without join, then one WHERE IN query with all ids from the collection.
        The latter obviously does a join and retrieves everything in one - more complex - query.

        Bonus points if you can loadRelated multiple relations at once.

        Show
        Jordi Boggiano added a comment - - edited Sample: $result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult(); $result->loadRelated('roles'); // loads all a.roles Would be the equivalent of: $result = $queryBuilder->select('a, r')->from('User', 'a')->join('a.roles', 'r')->getQuery()->getResult(); Except that the above does one simple query without join, then one WHERE IN query with all ids from the collection. The latter obviously does a join and retrieves everything in one - more complex - query. Bonus points if you can loadRelated multiple relations at once.
        Hide
        Christophe Coevoet added a comment -

        I see an issue here: if you do a WHERE IN with the multiple ids, how do you know which entity the role is related to ?

        and btw, the interface you suggested above would require breaking the BC: ``$result`` is an array right now.

        Show
        Christophe Coevoet added a comment - I see an issue here: if you do a WHERE IN with the multiple ids, how do you know which entity the role is related to ? and btw, the interface you suggested above would require breaking the BC: ``$result`` is an array right now.
        Hide
        Jordi Boggiano added a comment -

        The interface is just an example mimicking the way it worked in D1, take it with a grain of salt.

        As for the implementation, if you assume the roles table has a user_id and role column, then you can do WHERE user_id IN (1, 2, 3) and you'll get back the user ids so you know where to attach them. It might still require some joining in some cases, but the point is to keep the joins out of the main query.

        Show
        Jordi Boggiano added a comment - The interface is just an example mimicking the way it worked in D1, take it with a grain of salt. As for the implementation, if you assume the roles table has a user_id and role column, then you can do WHERE user_id IN (1, 2, 3) and you'll get back the user ids so you know where to attach them. It might still require some joining in some cases, but the point is to keep the joins out of the main query.
        Hide
        Guilherme Blanco added a comment -

        The one to be implemented would be:

        $result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult();
        $em->loadRelated($result, 'roles'); // loads all a.roles
        

        The reason for that is not all the times you have a PersistentCollection. You may have an ArrayCollection too.
        I just don't know yet how to handle array and ArrayCollection situations, since you may not know which class you're trying to fetch.
        Maybe I can try to grab the first item of array and retrieve Association information from ClassMetadata retrieved via get_class on first item. That would solve the problem.

        Any other ideas, feel free to give me.

        Show
        Guilherme Blanco added a comment - The one to be implemented would be: $result = $queryBuilder->select('a')->from('User', 'a')->getQuery()->getResult(); $em->loadRelated($result, 'roles'); // loads all a.roles The reason for that is not all the times you have a PersistentCollection. You may have an ArrayCollection too. I just don't know yet how to handle array and ArrayCollection situations, since you may not know which class you're trying to fetch. Maybe I can try to grab the first item of array and retrieve Association information from ClassMetadata retrieved via get_class on first item. That would solve the problem. Any other ideas, feel free to give me.
        Benjamin Eberlei made changes -
        Field Original Value New Value
        Workflow jira [ 13496 ] jira-feedback [ 14026 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback [ 14026 ] jira-feedback2 [ 15890 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 15890 ] jira-feedback3 [ 18146 ]
        Hide
        Vasily Khayrulin added a comment -

        Any news? it's really cool to use "WHERE primary_key IN " instead of joins.

        Show
        Vasily Khayrulin added a comment - Any news? it's really cool to use "WHERE primary_key IN " instead of joins.

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={query=DDC-1681, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Benjamin Eberlei
          • Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated: