【发布时间】:2015-12-03 20:36:00
【问题描述】:
我有两个实体:Ad 和 AdPhoto。它们有关系:OneToMany(许多 AdPhoto 到一个广告)。
在坚持之后,我尝试通过方法Ad::getPhoto()从Ad获取AdPhoto,但是我得到了PersistentCollection类,我不知道如何处理它。
帮助我了解如何将所有相关的 AdPhoto 转换为 Ad。
实体广告:
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;
...
}
实体广告照片:
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
));
【问题讨论】:
-
编辑问题并输入您尝试过的代码