1

My problem is related to the lazy-loading functionality of Doctrine 2.

Let's say I have these 2 Entities:

  • Area
  • Venue

Here are quick specs:

  • An Area can contain other areas (sub-areas...)
  • A Venue is located in only 1 area
  • Area::getFullName() should output "Parent area name (if any) > Area name"

My PHP Entities are:

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;

Let's say that area "Paris" contains a subarea named "Center"

If I call:

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

So far, so good.

But let's say now, that the "Fouquet's" restaurant is one Venue in the center of Paris:

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

The parent area name (--> "Paris") is not output...

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

But the parent Area Proxy Object is not NULL. It is just not initialized. And so calling the property "name" returns NULL.

It seems that "double" (or "many-to-one of many-to-one"...) lazy loading fails. Something like:

$venue->getArea()->get(Parent)Area()->name
eightyfive
  • 4,601
  • 3
  • 35
  • 44

1 Answers1

0

Properties should never be public when using Doctrine. This will lead to bugs with the way lazy loading works in Doctrine.

Source: Doctrine's Getting Started


Basically, you should add a getName() method to your Area class and use ->getName() instead of ->name so Doctrine can intercept the call and load the Proxy object's data. ;-)

mbinette
  • 5,094
  • 3
  • 24
  • 32
  • Oh, also, does this mean I have to implement getters and setters for all my (`protected`, `private`) properties? Thanks! – eightyfive Oct 21 '12 at 02:16
  • "does this mean I have to implement getters and setters for all my (protected, private)" - Yes, if you want to use Doctrine's default lazy loading. But you can redefine the Proxy(ies) and make them work another way. – mbinette Oct 21 '12 at 02:31