Doctrine MongoDB ODM 加载参考文档

发布于 2024-11-08 22:09:25 字数 1408 浏览 0 评论 0原文

我的详细情况是,iv'e 得到了一份用户文档:

/** @Document(collection="user") */
class User
{
    /** @Id */
    private $id;

    /** @ReferenceMany(targetDocument="Pet") */
    private $pet;

    public function getPet()
    {
        return $this->pet;
    }
}

并且 iv'e 得到了一份宠物文档:

/** @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());

结果是布尔值(false)。

有什么想法吗?

my szenario in detail, iv'e got one User Document:

/** @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. The method getMongoData returns the following data (which is correct):

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡月浅 2024-11-15 22:09:25

尝试重新生成水化器类。

try regenerating hydrator classes.

情话已封尘 2024-11-15 22:09:25

如果您的模型看起来像这样,

/**
 * @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()模型代理对象来检索引用的模型。它们包含许多递归引用,尝试将它们呈现为输出时,您将耗尽可用内存。

Provided your model looks something like this

/**
 * @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. These contain many recursive references and you will exhaust your available memory attempting to render them as output.

终止放荡 2024-11-15 22:09:25

从引用集合中获取单个对象的解决方案:

$pets = $this->user->getPets();     
if(!is_null($pets) && $pets->count() > 0) {
   $this->pet = $pets->first();
}

最大的学习(感谢@Phil):

不要尝试 var_dump() 或
print_r() 模型代理对象。
其中包含很多递归
参考文献,你会耗尽你的
尝试渲染的可用内存
它们作为输出。

此致,
斯蒂芬

Solution to get a single object from a referenced collection:

$pets = $this->user->getPets();     
if(!is_null($pets) && $pets->count() > 0) {
   $this->pet = $pets->first();
}

Biggest learning (thanks to @Phil):

Do not attempt to var_dump() or
print_r() the model proxy objects.
These contain many recursive
references and you will exhaust your
available memory attempting to render
them as output.

Best regards,
Stephan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文