DDC-1632: one-to-many associations with fetch mode eager using too many SQL queries #2050

Closed
opened 2026-01-22 13:38:41 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 2, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user ausi:

I am loading a list of many entities.
These entities have a one-to-many association to other entities.
These other entities should be loaded in one single SQL query (instead of one query for every entity in the first list).

As discribed in the doctrine2 documentation: http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql this should be possible with "EAGER" loading.

but it does not work as described.

my code:

    /****
     \* @ORM\OneToMany(targetEntity="Address", mappedBy="user", indexBy="id", fetch="EAGER")
     _/
    protected $addresses;
    public function *_construct(){
        $this->addresses = new ArrayCollection();
    }
}

class Address{
    /****
     \* @ORM\ManyToOne(targetEntity="User", inversedBy="addresses")
     \* @ORM\JoinColumns({
     \*   @ORM\JoinColumn(name="UserId", referencedColumnName="id")
     \* })
     */
    private $user;
}

class UserRepository{
    public function findUsersWithAddresses(){
        return $this->getEntityManager()
            ->createQuery('SELECT u FROM MyBundle:User u ORDER BY u.name ASC')
            ->setFetchMode('MyBundle\Entity\User', 'addresses', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
            ->setMaxResults(10)
            ->getResult();
    }
}

The method UserRepository::findUsersWithAddresses() executes 11 SQL Queries instead of 2 SQL Queries.

I am using:

  • symfony v2.0.9
  • doctrine-common 2.1.4
  • doctrine-dbal 2.1.5
  • doctrine 2.1.5
Originally created by @doctrinebot on GitHub (Feb 2, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user ausi: I am loading a list of many entities. These entities have a one-to-many association to other entities. These other entities should be loaded in one single SQL query (instead of one query for every entity in the first list). As discribed in the doctrine2 documentation: http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql this should be possible with "EAGER" loading. but it does not work as described. my code: ```class User{ /**** \* @ORM\OneToMany(targetEntity="Address", mappedBy="user", indexBy="id", fetch="EAGER") _/ protected $addresses; public function *_construct(){ $this->addresses = new ArrayCollection(); } } class Address{ /**** \* @ORM\ManyToOne(targetEntity="User", inversedBy="addresses") \* @ORM\JoinColumns({ \* @ORM\JoinColumn(name="UserId", referencedColumnName="id") \* }) */ private $user; } class UserRepository{ public function findUsersWithAddresses(){ return $this->getEntityManager() ->createQuery('SELECT u FROM MyBundle:User u ORDER BY u.name ASC') ->setFetchMode('MyBundle\Entity\User', 'addresses', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER) ->setMaxResults(10) ->getResult(); } } ``` The method UserRepository::findUsersWithAddresses() executes 11 SQL Queries instead of 2 SQL Queries. I am using: * symfony v2.0.9 * doctrine-common 2.1.4 * doctrine-dbal 2.1.5 * doctrine 2.1.5 ```
admin added the Bug label 2026-01-22 13:38:41 +01:00
admin closed this issue 2026-01-22 13:38:41 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 6, 2012):

Comment created by @beberlei:

Increased priority

@doctrinebot commented on GitHub (Feb 6, 2012): Comment created by @beberlei: Increased priority
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2012):

Comment created by ausi:

there is also a question on stackoverflow about this issue: [http://stackoverflow.com/questions/9020433/doctrine2-loads-one-to-many-associations-with-fetch-mode-eager-using-too-many-sq]

@doctrinebot commented on GitHub (Feb 9, 2012): Comment created by ausi: there is also a question on stackoverflow about this issue: [http://stackoverflow.com/questions/9020433/doctrine2-loads-one-to-many-associations-with-fetch-mode-eager-using-too-many-sq]
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2012):

Comment created by @beberlei:

Its not a bug, its just a feature we haven't implemented yet.

There is a ticket for it

@doctrinebot commented on GitHub (Feb 9, 2012): Comment created by @beberlei: Its not a bug, its just a feature we haven't implemented yet. There is a ticket for it
Author
Owner

@doctrinebot commented on GitHub (Feb 9, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Feb 9, 2012): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Feb 10, 2012):

Comment created by ausi:

Sorry, I didn't found the ticket before I reported this issue.
now i found it :) [http://www.doctrine-project.org/jira/browse/DDC-1149]

Would be very great to have this feature in a future version of doctrine.

@doctrinebot commented on GitHub (Feb 10, 2012): Comment created by ausi: Sorry, I didn't found the ticket before I reported this issue. now i found it :) [http://www.doctrine-project.org/jira/browse/[DDC-1149](http://www.doctrine-project.org/jira/browse/DDC-1149)] Would be very great to have this feature in a future version of doctrine.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2050