【发布时间】:2023-03-19 10:37:01
【问题描述】:
我是 Symfony2 的新手,正在开发我的第一个项目
- 1 个实体“ejuridique”,与 1 个单机关系
- 1 个实体“projet”与 1 个单机关系
- 1 个实体“照片”。
我想要一张表格,为每个“ejuridique”显示 ejuridique 名称和他的第一个项目的第一张照片。
但是,目前,当我使用 2 个级联 leftjoin 从控制器调用我自己的函数时,我收到错误“未定义的索引:chl\websitesBundle\photo”。
树枝“liste.html.twig”
{% for ejuridique in ejuridiques %}
<div>
<div>{{ejuridique.nom}} </div>
<div>
{% for projet in ejuridique.projets %}
{% for photo in projet.photos %}
{{photo.name}}
{%endfor%}
{%endfor%}
</div>
<div>
{%endfor%}
控制器
class websitesController extends Controller
{
public function listeAction($page)
{
$em = $this ->getDoctrine()
->getManager();
$ejuridiques = $em ->getRepository('chlwebsitesBundle:Ejuridique')
->findAll();
$projets = $em ->getRepository('chlwebsitesBundle:Ejuridique')
->myFindAll();
return $this->render('chlwebsitesBundle:websites:liste.html.twig', array(
'ejuridiques'=>$ejuridiques,'projets'=>$projets));
ejuridique 存储库
class ejuridiqueRepository extends EntityRepository
{
public function myFindAll()
{
$qb = $this->createQueryBuilder('a')
->leftjoin('a.projets', 'pr' )
->leftjoin('pr.photos', 'ph' )
->where('pr.projetPosition = :position_pr')
->ANDwhere('ph.position = :position_ph')
->setParameters(array('position_pr'=> "1",'position_ph'=> "1"))
->addSelect('pr')
->addSelect('ph');
return $qb->getQuery()
->getResult();
}
}
我的 3 个实体
法律
class ejuridique
{
/**
* @ORM\OneToMany(targetEntity="chl\websitesBundle\Entity\Projet", mappedBy="ejuridique")
* @ORM\JoinColumn(nullable=false)
*/
private $projets;
项目
class projet
{
/**
* @ORM\ManyToOne(targetEntity="chl\websitesBundle\Entity\ejuridique", inversedBy="projets")
*/
private $ejuridique;
/**
* @var integer
*
* @ORM\Column(name="projet_position", type="integer")
*/
private $projetPosition;
/**
* @ORM\OneToMany(targetEntity="chl\websitesBundle\Entity\Photo", mappedBy="projet")
* @ORM\JoinColumn(nullable=false)
*/
private $photos;
照片
class photo
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="chl\websitesBundle\Entity\projet", inversedBy="photos")
*/
private $projet;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer")
*/
private $position;
}
因此,对于所有这些文件,我暂时收到错误“ContectErrorException:注意:未定义索引:chl\websitesBundle\Entity\Photo”但是
当我从控制器调用我的 ejuridique 存储库中的简单“findall()”时,我得到了所有 ejuridique 及其所有项目和所有图片。因此,我认为我的实体和数据库一切正常。
当我调用我的函数“myFindAll”时,在“ejuridique”和“projet”之间只有 1 个leftjoin 参数时也是如此。一切正常。
只有在我自己的函数“myFindAll”中有 2 个级联 leftjoin 时,我才会收到此错误
我希望我的问题很清楚,并且我已经提供了所有必要的信息。
提前感谢您的帮助。
【问题讨论】:
标签: symfony doctrine-orm entity one-to-many