Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Doctrine Mongo ODM参考3系列_Mongodb_Doctrine Odm_Odm - Fatal编程技术网

Mongodb Doctrine Mongo ODM参考3系列

Mongodb Doctrine Mongo ODM参考3系列,mongodb,doctrine-odm,odm,Mongodb,Doctrine Odm,Odm,嗨,我正试图引用3个集合,但由于2>3个引用失败,让我解释一下我正在尝试做什么 我有一个用户类,它有referenceMany>posts,在posts中我有referenceMany>comments 注意:Mongo ODM+Zend框架2原则 这有点像用户写一篇帖子,然后有人对它发表评论 /** @MongoDB\Document */ class Users { /** @MongoDB\ReferenceMany(targetDocument="Posts", mappedB

嗨,我正试图引用3个集合,但由于2>3个引用失败,让我解释一下我正在尝试做什么

我有一个用户类,它有referenceMany>posts,在posts中我有referenceMany>comments

注意:Mongo ODM+Zend框架2原则

这有点像用户写一篇帖子,然后有人对它发表评论

/** @MongoDB\Document */
class Users {

    /** @MongoDB\ReferenceMany(targetDocument="Posts", mappedBy="user") */
    private $posts;

}

/** @MongoDB\Document */
class Posts {

    /** @MongoDB\Id */
    private $wallPostId;

    /** @MongoDB\ReferenceOne(targetDocument="Users", inversedBy="posts") */
    private $user;

    /** @MongoDB\ReferenceMany(targetDocument="Comments", mappedBy="post") */
    private $comments;

    /** @MongoDB\String */
    private $content;

}

/** @MongoDB\Document */
class Comments {

    /** @MongoDB\Id */
    private $commentId;

    /** @MongoDB\ReferenceOne(targetDocument="Posts", inversedBy="comments") */
    private $post;

    /** @MongoDB\String */
    private $content;

}
我正在尝试使用这段代码来获取任何帖子的所有评论

$post = $wallPostsAction->getWallPostById("5051d2a1e71a382c1b000000");
$output = "";
$output .= "Wall post content: " . $post->getContent() . "<br>";
//      $comment = new Comments();
//      $comment->setContent("Nice topic!");
//      $comment->setPost($post);
//      $this->dm->persist($comment);
//      $this->dm->flush();

echo count($post->getComments());
foreach($post->getComments() as $comment){
    $output .= " comment: " . $comment->getComment() . "<br>";
}

问题是为什么当我尝试获取特定帖子的评论时,我的评论计数为0,但当我尝试从用户获取帖子时,我可以获取它们。

,因为getComments()返回的只是指向该集合的光标。在您开始迭代该集合之前,条令不会运行任何查询来获取信息

将其添加到getWallPostById方法中的查询中

->field('comments')->prime(true)
这将告诉doctrine在获取帖子时获取关于评论的数据,然后getComments中的游标应该包含您要查找的计数信息


您是说我必须为此创建自定义查询?“回音计数($post->getComments());”怎么样getterIt看起来在getWallPostById中已经有一个查询了?可能在存储库类中?我的意思是,如果您将主选择添加到查询中,它将告诉doctrine在第一次查询中获取有关该引用的所有信息,而不是等到您想要访问该引用时再进行。这就是为什么你的“回音计数($post->getComments());”第一次不起作用,因为没有prime操作,它没有任何关于注释的信息。如果你在foreach声明后重复这一点,我想你会得到你期望的答案,因为你已经使用了references,但这将始终用post加载注释,这不是延迟加载,对吗?intead get引用将加载所有帖子的所有评论。正确。如果您知道您将迭代所有注释,那么您应该使用prime,以便在两次访问数据库时获得所需的所有信息。否则就是#评论+1次旅行。如果您仍然需要注释计数,那么在访问DB获取注释之前,您必须将计数存储在post文档中。这会导致更多的文章,因为你必须为每一条评论更新帖子。我只是重新阅读了这个问题。否,它将只加载引用该帖子的所有评论。如果您知道在找到该帖子后将使用getComments方法。您应该使用prime选项,因为这是将数据返回到应用程序的最佳方式。
->field('comments')->prime(true)