How to retrieve an entity with all of its associations using EntityManager in Doctrine2?

前端 未结 4 527
夕颜
夕颜 2020-12-30 14:52

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.

4条回答
  •  猫巷女王i
    2020-12-30 15:45

    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 Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER. If you're using the annotations, you can achieve this with:

    e.g.

    /**
     * @ManyToMany(targetEntity="Item", fetch="EAGER")
     */
    private $items;
    

提交回复
热议问题