Answer the question
In order to leave comments, you need to log in
Symfony 2 docnrine and fetch related entities?
I just encountered Symfony 2, so there was a slight difficulty due to ignorance of all the subtleties, the actual question is:
There are 3 entities Login, Campain, Banner, they are respectively interconnected Login has an ArrayCollection with associated Campain, it in turn has a collection with its Banner
/**
* @ORM\OneToMany(targetEntity="Campain", mappedBy="Login", indexBy="CampaignID", cascade={"persist", "remove" })
*/
protected $Campains;
/**
* @ORM\OneToMany(targetEntity="Banner", mappedBy="Campain", indexBy="BannerID", cascade={"persist", "remove" })
*/
protected $Banners;
$this->getEntityManager()->createQuery('
SELECT Login, Campain, Banner
FROM BroAppBundle:Login Login
LEFT JOIN Login.Campains Campain
LEFT JOIN Campain.Banners Banner
WHERE Login.User=:user_id')
->setParameter('user_id', $user_id)->getResult();
foreach($Logins as $Login){
foreach($Login->getCampains() as $Campain){
$Campain->GetBanners();
}
}
Answer the question
In order to leave comments, you need to log in
you can specify the EAGER selection method in the relationship description, in which your related entities will always be selected via outer join, although it is better to specify this where necessary, and precisely for the query.
$this->getEntityManager()->createQuery('
SELECT l FROM BroAppBundle:Login l
WHERE Login.User=:user_id')
->setParameter('user_id', $user_id)
->setFetchMode("BroAppBundle:Login", "Campains", "EAGER")
->setFetchMode("BroAppBundle:Login", "Banners", "EAGER")
->getResult();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question