public function addArticle(CmsArticle $article) { $this->articles[] = $article; $article->setAuthor($this); }
public function testUnsetFromReferencedCollectionWithoutCascade() { $articleRepository = $this->dm->getRepository('Documents\\CmsArticle'); $commentRepository = $this->dm->getRepository('Documents\\CmsComment'); // CmsArticle owns the one-to-many relationship but does not cascade $article = new CmsArticle(); $comment1 = new CmsComment(); $comment2 = new CmsComment(); $article->addComment($comment1); $article->addComment($comment2); /* Note: if we don't persist the CmsComments, CmsArticle's will create a * collection of two DBRefs with null $id values. Later on, this data is * used to initialize the PersistentCollection's mongoData property, and * leads to odd behavior (e.g. count is 2, but after unsetting the first * element, count becomes 0). */ $this->dm->persist($article); $this->dm->persist($comment1); $this->dm->persist($comment2); $this->dm->flush(); $this->dm->clear(); $article = $articleRepository->find($article->id); $comment1 = $commentRepository->find($comment1->id); $comment2 = $commentRepository->find($comment2->id); unset($article->comments[0]); $this->dm->flush(); $this->dm->clear(); $article = $articleRepository->find($article->id); $comment1 = $commentRepository->find($comment1->id); $comment2 = $commentRepository->find($comment2->id); // Removing reference on owner does not cause referenced document to be removed $this->assertNotNull($article); $this->assertCount(1, $article->comments); $this->assertNotNull($comment1); $this->assertNotNull($comment2); $this->dm->remove($article); $this->dm->flush(); $this->dm->clear(); $article = $articleRepository->find($article->id); $comment1 = $commentRepository->find($comment1->id); $comment2 = $commentRepository->find($comment2->id); // Remove does not cascade to referenced documents $this->assertNull($article); $this->assertNotNull($comment1); $this->assertNotNull($comment2); }