Doctrine2 does not load the collection before calling the method

When does Doctrine2 load an ArrayCollection?
Until I call a method like count or getValues, I have no data
Here is my business. I have a delegation object with a OneToMany (bidirectional) relation to the Promotion object, for example:

Promotion.php

use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Promotion { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Delegation", inversedBy="promotions", cascade={"persist"}) * @ORM\JoinColumn(name="delegation_id", referencedColumnName="id") */ protected $delegation; } 

Delegation.php

 use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity */ class Delegation { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\OneToMany(targetEntity="Promotion", mappedBy="delegation", cascade={"all"}, orphanRemoval=true) */ public $promotions; public function __construct() { $this->promotions = new \Doctrine\Common\Collections\ArrayCollection(); } } 

Now I am doing something like the following (with a specific delegation)

 $promotion = new Promotion(); $promotion = new Promotion(); $promotion->setDelegation($delegation); $delegation->addPromotion($promotion); $em->persist($promotion); $em->flush(); 

Finding relationships in the database is fine. I have a promotion line with a delegation rule. And now my problem arises: if I ask for $ delega-> getPromotions (), I get an empty PersistenCollection, but if I ask for the collection method, for example $ delegation> getPromotions () โ†’ count (), everything will be OK from here. I get the number correctly. By now setting $ delegation-> getPromotions () after this, I also get the PersistenCollection correctly.
Why is this happening? When does Doctrine2 load a collection?

Example:

 $delegation = $em->getRepository('Bundle:Delegation')->findOneById(1); var_dump($delegation->getPromotions()); //empty var_dump($delegation->getPromotions()->count()); //1 var_dump($delegation->getPromotions()); //collection with 1 promotion 

I could request directly the stock โ†’ getValues โ€‹โ€‹() and get it in order, but I would like to know what is happening and how to fix it.

As flu explains here, Doctrine2 uses proxy classes for lazy loading almost everywhere. But accessing $ delegation-> getPromotions () should automatically call the appropriate selection.
var_dump gets an empty collection, but, for example, uses it in a foreach statement, it works fine.

+4
source share
1 answer

The $delegation->getPromotions() call returns only the uninitialized Doctrine\ORM\PersistentCollection object. This object is not part of the proxy (if the loaded object is a proxy server).

Refer to the Doctrine\ORM\PersistentCollection API for how it works.

In principle, the collection itself is again a proxy server (in this case, the owner of the value) of the real wrapped ArrayCollection , which remains empty until any method in PersistentCollection called. In addition, ORM tries to optimize cases when your collection is marked as EXTRA_LAZY , so that it does not load even when certain operations are applied to it (for example, deleting or adding an item).

+5
source

All Articles