Using Symfony 2.8.
I have Community and MenuItem entities, where a Community has a set of MenuItems.
Community.php has the following code:
...
/**
* @ORM\OneToMany(targetEntity="MenuItem", mappedBy="community", fetch="LAZY")
* @ORM\OrderBy({"sequence" = "ASC"})
*/
private $menuItems;
...
MenuItem.php has the following code:
...
/**
* @var Community
*
* @ORM\ManyToOne(targetEntity="Community", inversedBy="menuItems")
*/
private $community;
...
The point is, when I use:
$menuItems = $community->getMenuItems();
the $menuItems
variable will be an empty collection.
The problem can be solved by setting fetch="EAGER"
instead of fetch="LAZY"
, because in that way the $menuItems
attribute of the Category entity is loaded immediatly.
LAZY vs EAGER (source) :
Whenever you have a managed entity instance at hand, you can traverse and use any associations of that entity that are configured LAZY as if they were in-memory already. Doctrine will automatically load the associated objects on demand through the concept of lazy-loading.
Whenever you query for an entity that has persistent associations and these associations are mapped as EAGER, they will automatically be loaded together with the entity being queried and is thus immediately available to your application.
The point is that while EAGER loading is working as expected, LAZY loading seems not working at all. Any idea about why?