Symfony 2: getReference and find

Using an object manager getReference()

or find()

method returns an uninitialized object for some database records. Do you know why and what to do?

+3


source to share


1 answer


getReference()

does not load the object if not loaded yet, it only returns the proxy object.

find()

returns the loaded object.

CFR. 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 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 retrieve objects to use them, always use find()

.

+14


source







All Articles