Using the entity manager getReference()
or find()
method returns a non initialized object for some records of the database. Do you know why and what should be done?
getReference()
does not load the object if it has not been loaded yet, it only returns a proxy to the object.
find()
returns a loaded object.
cfr. the documentation:
// this call does not trigger a db query, but creates an empty proxy with the ID
$objectA = $this->entityManager->getReference('EntityName', 1);
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $objectA); // === true
// this will trigger a query, loading the state that's configured to eager load
// since the UnitOfWork already has a proxy, that proxy will be reused
$objectB = $this->entityManager->find('EntityName', 1);
$this->assertSame($objectA, $objectB); // === true
getReference()
exists for special use cases, if you are fetching objects to use them, always use find()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With