我得到了正确运行的以下查询:
$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
}
我收到以下错误消息: “体验”上的未知记录属性/相关组件“jobtitle”
有人知道吗?如何从左连接回显字段?
最佳答案
<?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 返回对象数组,并且连接表中的元素位于子数组下
关于php - Symfony 左连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4194514/