I am using the Cache Component for Symfony 3.2. I have an entity named MenuItem that has a many to one relationship with other:
class MenuItem
{
//...
/**
* @ORM\ManyToOne(targetEntity="BaseBundle\Entity\ProductCategory")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
//...
In a service, I want to retrieve all the menuitems and save them in the cache:
$menuItemsCached = $this->cache->getItem('app.menu_items');
if ($menuItemsCached->isHit()) {
$menuItems = $menuItemsCached->get();
} else {
$repository = $this->em->getRepository('WebBundle:MenuItem');
$menuItems = $repository->findBy([], ['weight' => 'ASC']);
$menuItemsCached->set($menuItems);
$this->cache->save($menuItemsCached);
}
return $menuItems;
The problem is that, when I get the array from the cache, the entities ProductCategory inside my MenuItems are proxies as you can see:
5 => MenuItem {#3428 ▼
#id: 3
#name: "Vinils"
#weight: 60
#selector: "vinil"
-categoryCollection: ProductCategory {#3429 ▼
+__isInitialized__: false
#id: 23
#name: ""
And if I called getName() it returns "" instead the Product Category name (lazy loading not working).
I don't understand this behaviour, so I would be very pleasant if anyone can explain me because lazy loading is not working (I suppose I can configure the relationship with fetch="EAGER" to avoid this issue).
Thanks.