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?
1 Answer
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()
.
getReference()
orfind()
methods of Doctrine returns a booleanfalse
value if the record is not found. Are you sure the record exists in the database? Can you show your attempts at the code?