【问题标题】:Symfony left join querySymfony 左连接查询
【发布时间】: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

标签: php mysql symfony1


【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 2021-12-25
      • 2017-06-30
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多