Symfony 2:getReference并找到

mlw*_*mos 5 symfony doctrine-orm

使用实体管理器getReference()find()方法为数据库的某些记录返回非初始化对象.你知道为什么和应该做什么吗?

NDM*_*NDM 25

getReference() 如果尚未加载对象,则不加载该对象,它只返回对象的代理.

find() 返回一个加载的对象.

(CFR).文件:

// 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
Run Code Online (Sandbox Code Playgroud)

getReference()对于特殊用例存在,如果要获取对象以使用它们,请始终使用find().