[英]Doctrine MongoDB ODM load referenced documents
my szenario in detail, iv'e got one User Document:我的 szenario 详细信息,iv'e 有一个用户文档:
/** @Document(collection="user") */
class User
{
/** @Id */
private $id;
/** @ReferenceMany(targetDocument="Pet") */
private $pet;
public function getPet()
{
return $this->pet;
}
}
and iv'e got one Pet document:我得到了一份宠物文件:
/** @Document(collection="pet") */
class Pet
{
/** @Id */
private $id;
/** @ReferenceMany(targetDocument="User") */
private $user;
public function getUser()
{
return $this->user;
}
}
A many to many correlation.多对多相关性。 If i call the following code for an existing document...
如果我为现有文档调用以下代码...
$result = $this->_dbContainer->getDocumentManager()->getRepository('User')->findBy(array('id' => => 'XZTZHJ323LKFHGJKLHGFGHJK'));
print_r($result->toArray());
...it ends in an endless loop. ...它以无限循环结束。 Error message:
错误信息:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 112721921 bytes) in ...
If i execute the following code:如果我执行以下代码:
var_dump($result->count());
The result ist one / it exist (everything ok).结果是一个/它存在(一切正常)。 A var_dump of $result->current() is NULL.
$result->current() 的 var_dump 是 NULL。 The method getMongoData returns the following data (which is correct):
getMongoData 方法返回以下数据(正确):
Array ( [0] => Array ( [$ref] => example [$id] => MongoId Object ( [$id] => 4ddac7667294c79e17000002 ) [$db] => test ) )
If i execute the following code:如果我执行以下代码:
var_dump($result->current());
The result is boolean (false).结果是 boolean(假)。
Any ideas?有任何想法吗?
try regenerating hydrator classes.尝试再生水合器类。
Provided your model looks something like this如果您的 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;
}
}
You should be able to retrieve the referenced models using您应该能够使用检索引用的模型
$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
}
Do not attempt to var_dump()
or print_r()
the model proxy objects.不要尝试
var_dump()
或print_r()
model 代理对象。 These contain many recursive references and you will exhaust your available memory attempting to render them as output.这些包含许多递归引用,您将用尽可用的 memory 试图将它们呈现为 output。
Solution to get a single object from a referenced collection:从引用集合中获取单个 object 的解决方案:
$pets = $this->user->getPets();
if(!is_null($pets) && $pets->count() > 0) {
$this->pet = $pets->first();
}
Biggest learning (thanks to @Phil):最大的学习(感谢@Phil):
Do not attempt to var_dump() or print_r() the model proxy objects.
不要尝试 var_dump() 或 print_r() model 代理对象。 These contain many recursive references and you will exhaust your available memory attempting to render them as output.
这些包含许多递归引用,您将用尽可用的 memory 试图将它们呈现为 output。
Best regards, Stephan最好的问候,斯蒂芬
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.