3
使用实体管理器getReference()
或find()
方法为数据库的某些记录返回一个未初始化的对象。你知道为什么和应该做什么?Symfony 2:getReference并找到
使用实体管理器getReference()
或find()
方法为数据库的某些记录返回一个未初始化的对象。你知道为什么和应该做什么?Symfony 2:getReference并找到
getReference()
如果尚未加载对象,则不会加载对象,它只会向对象返回代理。
find()
返回一个加载的对象。
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()
存在特殊用途的情况下,如果你是获取对象使用它们,总是使用find()
。
你有一段代码吗? – Fred 2014-10-08 12:43:21
如果没有找到记录,Doctrine的'getReference()'或'find()'方法返回一个布尔值'false'值。你确定该记录存在于数据库中吗?你可以在代码中显示你的尝试吗? – sjagr 2014-10-08 12:43:48
我猜对于find的非初始化意味着并不是所有的对象关联都被填充了? – Cerad 2014-10-08 14:42:20