我想获取嵌入式文档的集合。我像这样将数据放入数据库:
$dm->getRepository('BundleUserBundle:User')->addRatedPostById($user->getId(), new RatedPost($id, $type));
...效果很好。现在我在 mongo 控制台中看到了新数据,但是当我尝试通过该getRatedPosts()
方法获取这些数据时,它返回null而不是 ArrayCollection。怎么了?
上下文如下所示:
class User extends BaseUser {
/**
* @MongoDB\EmbedMany(targetDocument="RatedPost")
*/
protected $ratedPosts;
/**
* Add ratedPosts
*
* @param Bundle\UserBundle\Document\RatedPost $ratedPosts
*/
public function addRatedPost(\Bundle\UserBundle\Document\RatedPost $ratedPosts)
{
$this->ratedPosts[] = $ratedPosts;
}
/**
* Remove ratedPosts
*
* @param Bundle\UserBundle\Document\RatedPost $ratedPosts
*/
public function removeRatedPost(\Bundle\UserBundle\Document\RatedPost $ratedPosts)
{
$this->ratedPosts->removeElement($ratedPosts);
}
/**
* Get ratedPosts
*
* @return Doctrine\Common\Collections\Collection $ratedPosts
*/
public function getRatedPosts()
{
return $this->ratedPosts;
}
/**
* @MongoDB\EmbeddedDocument
*/
class RatedPost
{
/**
* @MongoDB\Int
*/
public $post;
/**
* @MongoDB\String
*/
public $rate;
...
}