Doctrine 2方法有时会返回关系,有时则不会

时间:2012-06-22 09:44:50

标签: php orm symfony doctrine-orm

我一直在使用Doctrine 2和Symfony 2(顺便说一下很棒的框架)几天,我设法让它在我的管理员后端的控制器中工作:

    $intern = $em->getRepository('CrpImgInternBundle:Intern')->find($id);


    $currentSemester = $em->getRepository('CrpImgAdminBundle:Semester')->getCurrent();

    $qualifiedInternships = $intern->getFilteredInternships(InternInternship::STATUS_QUALIFIED, $currentSemester);
    $appliedInternships = $intern->getFilteredInternships(InternInternship::STATUS_APPLIED, $currentSemester);
    $currentInternships = $intern->getFilteredInternships(InternInternship::STATUS_CURRENT, $currentSemester);
    $doneInternships = $intern->getFilteredInternships(InternInternship::STATUS_DONE, $currentSemester);

getFilteredInternships方法在我的Intern实体中定义并返回一个数组:

public function getFilteredInternships($status, $semester) {
    $result = array();

    foreach ($this->getInternInternships() as $internInternship) {
        if ($internInternship->getStatus() == $status
        && $internInternship->getSemester()->getId() == $semester->getId()
        ) {
            $result[] = $internInternship->getInternship();
        }
    }

    return $result;
}

internInternships协会是一对多的

 /**
 * @ORM\OneToMany(targetEntity="InternInternship", mappedBy="intern")
 */
  private $intern_internships;

编辑:

反向关联:

/**
 * @ORM\ManyToOne(targetEntity="Intern", inversedBy="intern_internships")
 * @ORM\JoinColumn(name="intern_id", referencedColumnName="id")
 */
private $intern;

但是当我在我的前端的另一个控制器中使用它时(用户登录)它不会返回任何内容:

    $user = $this->container->get('security.context')->getToken()->getUser();
    $intern = $em->getRepository('CrpImgInternBundle:Intern')->find($user->getId());

    $currentSemester = $em->getRepository('CrpImgAdminBundle:Semester')->getCurrent();
    $qualifiedInternship = $intern->getFilteredInternships($currentSemester, InternInternship::STATUS_QUALIFIED);

我想外国实体没有被提取,但我无法理解为什么它在我的后端工作,我想知道原因。

感谢。

0 个答案:

没有答案