5

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?

3
  • Do you have a code snippet?
    – Fred
    Oct 8, 2014 at 12:43
  • The getReference() or find() methods of Doctrine returns a boolean false value if the record is not found. Are you sure the record exists in the database? Can you show your attempts at the code?
    – sjagr
    Oct 8, 2014 at 12:43
  • I'm guessing that non-initialized for find means that not all of the objects associations are filled in?
    – Cerad
    Oct 8, 2014 at 14:42

1 Answer 1

27

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.