Symfony 2查询Doctrine映射对象

时间:2012-01-16 01:26:27

标签: symfony doctrine-orm

在查询控制器中对象的信息时遇到问题。如果我需要扩展所提供的信息,请告诉我(我在上下文之外更改了对象的名称)。

我有两个实体的oneToOne双向映射,这两个实体位于不同的包中。

对象1:

oneToOne:
  person:
    targetEntity: Program\PersonBundle\Entity\Person
    fetch: EAGER 
    inversedBy: group
    joinColumn:
      name: id
      referencedColumnName: id

对象2:

oneToOne:
  group:
    targetEntity: Program\GroupBundle\Entity\Group
    mappedBy: person
    fetch: EAGER

我在每个对象的每个Entity.php文件中都有适当的变量。我的问题是关于在我的控制器中访问此信息。当我尝试访问捆绑包中的信息时,它不会在第一次调用时抓取它。但是,当我再次调用它时,它会正确地抓取信息。有人知道为什么吗?不是'fetch:EAGER'应该解决这个问题吗?

$em = $this->getDoctrine()->getEntityManager();
$personsRepository =  $em->getRepository("PersonBundle:Person");

foreach($persons as $person)
{
     $person->getGroup()->getName(); // This doesn't return anything
     $person->getGroup()->getName(); // This returns the group name....
}

提前致谢!

0 个答案:

没有答案
相关问题