Krzysiek Gała
Krzysiek Gała

Reputation: 21

Get empty collection of embedded documents

I want to get a collection of embedded documents. I put data into the database like this:

$dm->getRepository('BundleUserBundle:User')->addRatedPostById($user->getId(), new RatedPost($id, $type));

...which works fine. Now I see new data in mongo console, but when I'm trying to get this data via the getRatedPosts() method, it return null instead of the ArrayCollection. Whats wrong?

Context looks like this:

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;
   ...
}

Upvotes: 2

Views: 559

Answers (1)

joeni
joeni

Reputation: 151

I had exactly the same problems: saving was no problem but fetching the embedded document was. Turned out it was a "Symfony" caching problem (also on app_dev.php). Have you tried to remove your cache, this worked for me!

Upvotes: 9

Related Questions