原则2 - 多对一延迟加载中的多对一失败

时间:2012-10-20 10:50:55

标签: doctrine-orm lazy-loading

我的问题与Doctrine 2的延迟加载功能有关。

假设我有这两个实体:

  • 地点

以下是快速规格:

  • 区域可以包含其他区域(子区域......)
  • A Venue仅位于1个区域
  • Area :: getFullName()应输出“父区域名称(如果有)>区域名称”

我的PHP实体是:

class Area extends AbstractEntity {
/**
 * @ORM\ManyToOne(targetEntity="Area", inversedBy="children")
 */
private $parent;

public function getFullName() {
    if (!isset($this->fullName)) {
        $this->fullName = ($this->getParent() ? $this->getParent()->name . ' > ' : '') . $this->name;
    }
    return $this->fullName;
}

class Venue extends AbstractEntity {

/** 
 * @ORM\ManyToOne(targetEntity="Area")
 */
private $area;

假设“巴黎”区域包含一个名为“中心”的子区域

如果我打电话:

$area = $repoArea->findByUrl("paris/center")
echo $area->getFullName();
// --> "Paris > Center"

到目前为止,非常好。

但现在让我们说,“Fouquet's”餐厅是巴黎市中心的一个场地:

$venue = $repoVenue->findByName("Fouquet's");
echo $venue->getArea()->getFullName()
// --> " > Center"

父区名称( - >“Paris”)未输出...

$this->fullName = ($this->getParent() ? $this->getParent()->name . ' > ' : '') . $this->name;

但是父区域代理对象不是NULL。它只是没有初始化。因此,调用属性“name”将返回NULL。

似乎“双”(或“多对一”多对一“......)延迟加载失败。类似的东西:

$venue->getArea()->get(Parent)Area()->name

1 个答案:

答案 0 :(得分:0)

  

使用Doctrine时,属性永远不应公开。这将导致   延迟加载在Doctrine中的工作方式。

来源: Doctrine's Getting Started


基本上,您应该在getName()课程中添加Area方法并使用->getName()代替->name,以便Doctrine可以拦截调用并加载代理对象&#39 ; s数据。 ; - )

相关问题