Symfony 2 : getReference and find

5

使用实体管理器的getReference()find()方法,对于数据库中的某些记录返回未初始化的对象。您知道为什么会这样,并应该采取什么措施吗?


你有代码片段吗? - Fred
Doctrine的getReference()find()方法在未找到记录时会返回布尔值false。您确定数据库中存在该记录吗?能否展示一下您的代码尝试? - sjagr
我猜测非初始化的find意味着并非所有对象的关联都被填充了? - Cerad
1个回答

27
getReference()不会加载尚未加载的对象,它只返回该对象的代理。而find()会返回已加载的对象。
参见文档
// 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()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接