Uploaded image for project: '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


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


      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.



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


            • Created: