2014-10-08 35 views
3

使用實體管理器getReference()find()方法爲數據庫的某些記錄返回一個未初始化的對象。你知道爲什麼和應該做什麼?Symfony 2:getReference並找到

+0

你有一段代碼嗎? – Fred 2014-10-08 12:43:21

+0

如果沒有找到記錄,Doctrine的'getReference()'或'find()'方法返回一個布爾值'false'值。你確定該記錄存在於數據庫中嗎?你可以在代碼中顯示你的嘗試嗎? – sjagr 2014-10-08 12:43:48

+0

我猜對於find的非初始化意味着並不是所有的對象關聯都被填充了? – Cerad 2014-10-08 14:42:20

回答

14

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