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