I have a simple entity with many-to-many and one-to-many associations. I’m aware of ‘Joins’ for fetching related associations which is a manual solution for my problem.
How can I fetch an entity with all of its associations using EntityManager in Doctrine2? e.g.:
$this->em ->getRepository('EntitiesPatientprofile') ->findOneByuserid('555555557') ->fetchAllAssociations();
Advertisement
Answer
Doctrine 2 uses Proxy classes for lazy loading, so you don’t actually need to have the associations’ data fetched until you use the objects. Since the Proxy classes inherit from your association classes, you’re able to use the proxies exactly as you would use the fretch association classes.
but, if you really need to fetch the actual association classes, you need to tell the query to set the fetch mode to DoctrineORMMappingClassMetadata::FETCH_EAGER. If you’re using the annotations, you can achieve this with:
e.g.
/** * @ManyToMany(targetEntity="Item", fetch="EAGER") */ private $items;