我有兩個實體:Ad
和AdPhoto
。他們有關係:OneToMany(許多AdPhoto到一個廣告)。 後堅持,我試圖通過方法Ad::getPhoto()
擺脫Ad
AdPhoto
,但我得到PersistentCollection
類,我不知道該怎麼辦。Symfony2從具有OneToMany關係的實體獲取數據
幫助我明白,我怎麼能得到所有相關AdPhoto到廣告。
實體廣告:
namespace AdBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Ad
*
* @ORM\Table(name="Ad")
* @ORM\Entity
*/
class Ad
{
...
/**
* @ORM\OneToMany(targetEntity="AdBundle\Entity\AdPhoto", mappedBy="id")
*/
private $photo;
...
}
實體AdPhoto:
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdPhoto
*
* @ORM\Table(name="AdPhoto")
* @ORM\Entity
*/
class AdPhoto
{
...
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\Ad", inversedBy="photo")
* @ORM\JoinColumn(name="ad", referencedColumnName="id")
*/
private $ad;
...
}
在控制器:
$ad = $this->getDoctrine()->getRepository('AdBundle:Ad')
->findOneBy(array(
'id' => $id
));
var_dump($ad->getPhoto());
return $this->render('AdBundle:Default:view.html.twig', array(
'ad' => $ad
));
編輯問題,並把你試過的代碼 –