[英]Doctrine MongoDB ODM load referenced documents
我的 szenario 詳細信息,iv'e 有一個用戶文檔:
/** @Document(collection="user") */
class User
{
/** @Id */
private $id;
/** @ReferenceMany(targetDocument="Pet") */
private $pet;
public function getPet()
{
return $this->pet;
}
}
我得到了一份寵物文件:
/** @Document(collection="pet") */
class Pet
{
/** @Id */
private $id;
/** @ReferenceMany(targetDocument="User") */
private $user;
public function getUser()
{
return $this->user;
}
}
多對多相關性。 如果我為現有文檔調用以下代碼...
$result = $this->_dbContainer->getDocumentManager()->getRepository('User')->findBy(array('id' => => 'XZTZHJ323LKFHGJKLHGFGHJK'));
print_r($result->toArray());
...它以無限循環結束。 錯誤信息:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 112721921 bytes) in ...
如果我執行以下代碼:
var_dump($result->count());
結果是一個/它存在(一切正常)。 $result->current() 的 var_dump 是 NULL。 getMongoData 方法返回以下數據(正確):
Array ( [0] => Array ( [$ref] => example [$id] => MongoId Object ( [$id] => 4ddac7667294c79e17000002 ) [$db] => test ) )
如果我執行以下代碼:
var_dump($result->current());
結果是 boolean(假)。
有任何想法嗎?
嘗試再生水合器類。
如果您的 model 看起來像這樣
/**
* @Document
*/
class User
{
/** @Id */
private $id;
/** @ReferenceMany(targetDocument="Something") */
private $somethings;
public function __construct()
{
$this->somethings = new \Doctrine\Common\Collections\ArrayCollection;
}
public function getSomethings()
{
return $this->somethings;
}
}
您應該能夠使用檢索引用的模型
$user = $dm->find('User', $id);
$somethings = $user->getSomethings();
$firstSomething = $somethings->current(); // will return false if empty, can also use first()
foreach ($somethings as $something) {
// and so on
}
不要嘗試var_dump()
或print_r()
model 代理對象。 這些包含許多遞歸引用,您將用盡可用的 memory 試圖將它們呈現為 output。
從引用集合中獲取單個 object 的解決方案:
$pets = $this->user->getPets();
if(!is_null($pets) && $pets->count() > 0) {
$this->pet = $pets->first();
}
最大的學習(感謝@Phil):
不要嘗試 var_dump() 或 print_r() model 代理對象。 這些包含許多遞歸引用,您將用盡可用的 memory 試圖將它們呈現為 output。
最好的問候,斯蒂芬
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.