Symfony 左连接查询
Posted
技术标签:
【中文标题】Symfony 左连接查询【英文标题】:Symfony left join query 【发布时间】:2010-11-16 13:18:15 【问题描述】:以下查询运行正常:
$q = $this->createQuery('e')
->where('e.Persons_idUser =?', $request)
->leftJoin('e.JobTitles jt')
->leftJoin('e.EmploymentLevels el');
但是当我遍历结果并尝试从左连接访问字段时:
foreach ($work as $w)
echo $w->employername;
echo $w->jobtitle; // this is from the left join
echo $w->employmentlevel; // this is from the left join
我收到以下错误消息: “经验”上的未知记录属性/相关组件“职位”
有人知道吗?如何回显左连接中的字段?
【问题讨论】:
你需要做类似$w->EmploymentLevels->employmentlevel
【参考方案1】:
<?php
foreach ($work as $w)
echo $w->employername;
foreach($w->JobTitles as $job)
echo $job->jobtitle; // this is from the left join
foreach($w->EmploymentLevels as $employ)
echo $employ->employmentlevel; // this is from the left join
?>
这将在 symfony 返回对象数组并且连接表中的元素位于子数组下时起作用
【讨论】:
【参考方案2】:解决方案:
foreach ($work as $w)
echo $w->employername;
echo $w->JobTitles->jobtitle; // this is from the left join
echo $w->EmploymentLevels->employmentlevel; // this is from the left join
【讨论】:
以上是关于Symfony 左连接查询的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 symfony 在教义查询构建器中选择表之间的特定连接?