Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Critical
-
Resolution: Invalid
-
Affects Version/s: 2.1.5
-
Fix Version/s: None
-
Component/s: ORM
-
Security Level: All
-
Labels:None
Description
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
Increased priority