Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 : getReference and find

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?

like image 469
mlwacosmos Avatar asked Oct 08 '14 12:10

mlwacosmos


1 Answers

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().

like image 154
NDM Avatar answered Sep 23 '22 00:09

NDM