Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2530

Fetch eager in onetomany is not storing the collection resulting in same query being executed twice

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.3
    • Fix Version/s: 2.3.5
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Windows 8

      Description

      I have two entities Users and Bugs (A user can report multiple bugs) sharing bidirectional onetomany relation between them. On the inverse side of the relation I have put fetch = eager as given below.

      class Users{
           /**
           * @ORM\OneToMany(targetEntity="Bugs", mappedBy="reporter",fetch="EAGER")
           * @var Bugs[]
           **/
           private $reportedBugs;
      }
      

      Now when I run this code:

      $user = $em->find("ZC\Entity\Users", 7);
      

      Firstly user entity is fetched from the user table with id = 7. Then it looks for the bugs associated with user_id = 7 in the bugs table. This seems to be the expected behavior where due to the eager fetch the related associations are automatically loaded. What seems to be the bug is, after this when the user's reported bugs are called, an unnecessary query is executed which again looks for the bugs in the bugs table related to the user_id = 7 and that is basically the same query as before. This is just a redundant query. So the eager fetch should have stored the associated bugs in the first call itself and the subsequent call to the reported bugs should just return the already fetched entities.

        Activity

        Koustubh Sinhal created issue -
        Hide
        Marco Pivetta added a comment -

        Koustubh Sinhal are you actually using 2.0 or is this on a newer branch? (2.3?)

        Show
        Marco Pivetta added a comment - Koustubh Sinhal are you actually using 2.0 or is this on a newer branch? (2.3?)
        Hide
        Koustubh Sinhal added a comment -

        Yup my bad, it is 2.3.3 version of doctrine. Atleast this is what i am seeing in the doctrine ORM version.php.

        Show
        Koustubh Sinhal added a comment - Yup my bad, it is 2.3.3 version of doctrine. Atleast this is what i am seeing in the doctrine ORM version.php.
        Koustubh Sinhal made changes -
        Field Original Value New Value
        Affects Version/s 2.3.3 [ 10329 ]
        Affects Version/s 2.0 [ 10021 ]
        Koustubh Sinhal made changes -
        Fix Version/s 2.0 [ 10021 ]
        Marco Pivetta made changes -
        Description I have two entities Users and Bugs (A user can report multiple bugs) sharing bidirectional onetomany relation between them. On the inverse side of the relation I have put fetch = eager as given below.
        class Users{
             /**
             * @ORM\OneToMany(targetEntity="Bugs", mappedBy="reporter",fetch="EAGER")
             * @var Bugs[]
             **/
             private $reportedBugs;
        }
        Now when I run this code:
        $user = $em->find("\ZC\Entity\Users",7);
        Firstly user entity is fetched from the user table with id = 7. Then it looks for the bugs associated with user_id = 7 in the bugs table. This seems to be the expected behavior where due to the eager fetch the related associations are automatically loaded. What seems to be the bug is, after this when the user's reported bugs are called, an unnecessary query is executed which again looks for the bugs in the bugs table related to the user_id = 7 and that is basically the same query as before. This is just a redundant query. So the eager fetch should have stored the associated bugs in the first call itself and the subsequent call to the reported bugs should just return the already fetched entities.
        I have two entities Users and Bugs (A user can report multiple bugs) sharing bidirectional onetomany relation between them. On the inverse side of the relation I have put fetch = eager as given below.

        {code}
        class Users{
             /**
             * @ORM\OneToMany(targetEntity="Bugs", mappedBy="reporter",fetch="EAGER")
             * @var Bugs[]
             **/
             private $reportedBugs;
        }
        {code}

        Now when I run this code:

        {code}
        $user = $em->find("ZC\Entity\Users", 7);
        {code}

        Firstly user entity is fetched from the user table with id = 7. Then it looks for the bugs associated with user_id = 7 in the bugs table. This seems to be the expected behavior where due to the eager fetch the related associations are automatically loaded. What seems to be the bug is, after this when the user's reported bugs are called, an unnecessary query is executed which again looks for the bugs in the bugs table related to the user_id = 7 and that is basically the same query as before. This is just a redundant query. So the eager fetch should have stored the associated bugs in the first call itself and the subsequent call to the reported bugs should just return the already fetched entities.
        Hide
        Benjamin Eberlei added a comment -

        verified

        Show
        Benjamin Eberlei added a comment - verified
        Hide
        Benjamin Eberlei added a comment -

        Fixed and scheduled for 2.3.4

        Show
        Benjamin Eberlei added a comment - Fixed and scheduled for 2.3.4
        Benjamin Eberlei made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.3.4 [ 10420 ]
        Resolution Fixed [ 1 ]
        Hide
        Koustubh Sinhal added a comment -

        Thanx

        Show
        Koustubh Sinhal added a comment - Thanx
        Benjamin Eberlei made changes -
        Fix Version/s 2.3.5 [ 10521 ]
        Fix Version/s 2.3.4 [ 10420 ]

        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-2530, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Koustubh Sinhal
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: