I'm using Doctrine with Symfony and the entity generator initializes collection in the entity's constructor. For example:
/**
* @var MyProject\MyBundle\Entity\Foo
*
* @ORM\ManyToMany(targetEntity="MyProject\MyBundle\Entity\Foo")
*/
private $foos;
public function __construct() {
$this->foos = new \Doctrine\Common\Collections\ArrayCollection();
}
And I can read in Doctrine's best practices that... it is a good practice to do so.
But the question is: what is the interest?
I don't see any change if I remove this line in the constructor. I guess it is lazy-loaded then, but is the performance drop significant enough to take the time to write the constuctor (the app/console doctrine:generate:entities
command doesn't feed the constructor with new ArrayCollection if the constructor already exist, so I have to do it manually).
And if it is really better to do it, should I do it for all relation or only the nullable=false
one?
Imagine the entity Car
in OneToMany with Wheel
(not nullable) and OneToMany with Passenger
(nullable). Should I initialyze only $wheels
or both? And why ?