public function testRepositoryMethodWithoutMappedBy()
 {
     $blogPost = new \Documents\BlogPost('Test');
     $blogPost->addComment(new \Documents\Comment('Comment', new \DateTime()));
     $this->dm->persist($blogPost);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $this->dm->createQueryBuilder('Documents\\BlogPost')->getQuery()->getSingleResult();
     $this->assertCount(1, $blogPost->repoCommentsWithoutMappedBy);
     $this->assertEquals('Comment', $blogPost->repoCommentsWithoutMappedBy[0]->getText());
 }
 public function testSetStrategy()
 {
     $repo = $this->dm->getRepository('Documents\\BlogPost');
     $blogPost = new \Documents\BlogPost('Test');
     $blogPost->addComment(new \Documents\Comment('Comment', new \DateTime()));
     $this->dm->persist($blogPost);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $this->dm->createQueryBuilder('Documents\\BlogPost')->getQuery()->getSingleResult();
     $this->assertEquals('Comment', $blogPost->repoCommentsSet[0]->getText());
 }
 public function testOneToOne()
 {
     $date1 = new DateTime();
     $date1->setTimestamp(strtotime('-20 seconds'));
     $date2 = new DateTime();
     $date2->setTimestamp(strtotime('-10 seconds'));
     $blogPost = new \Documents\BlogPost('Test');
     $blogPost->addComment(new \Documents\Comment('Comment 1', $date1));
     $blogPost->addComment(new \Documents\Comment('Comment 2', $date2));
     $this->dm->persist($blogPost);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $this->dm->createQueryBuilder('Documents\\BlogPost')->getQuery()->getSingleResult();
     $this->assertEquals('Comment 2', $blogPost->repoComment->getText());
     $this->assertEquals('Comment 1', $blogPost->repoComments[0]->getText());
     $this->assertEquals('Comment 2', $blogPost->repoComments[1]->getText());
 }
 /**
  * Tests Bi-Directional Reference "one to many" with nullable=true flag
  *
  * @url http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/bidirectional-references.html
  */
 public function testOneToMany()
 {
     $user = new User();
     $user->setUsername('w00ting');
     $this->dm->persist($user);
     $this->dm->flush();
     $post1 = new BlogPost();
     $post1->name = 'post1';
     $post1->setUser($user);
     $post2 = new BlogPost();
     $post2->name = 'post2';
     $post2->setUser($user);
     $post3 = new BlogPost();
     $post3->name = 'post3';
     $post3->setUser($user);
     $this->dm->persist($post1);
     $this->dm->persist($post2);
     $this->dm->persist($post3);
     $this->dm->flush();
     $post1->setUser(null);
     $this->dm->flush();
     $this->assertNull($post1->user);
 }
Ejemplo n.º 5
0
 public function testUnsetFromReferencedCollectionWithCascadeAndMappedBy()
 {
     $blogPostRepository = $this->dm->getRepository('Documents\\BlogPost');
     $commentRepository = $this->dm->getRepository('Documents\\Comment');
     /* CmsComment owns the one-to-many relationship, since BlogPost uses
      * mappedBy. Both sides cascade operations.
      */
     $blogPost = new BlogPost();
     $comment1 = new Comment('comment1', new \DateTime());
     $comment2 = new Comment('comment2', new \DateTime());
     $blogPost->addComment($comment1);
     $blogPost->addComment($comment2);
     $this->dm->persist($blogPost);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $blogPostRepository->find($blogPost->id);
     $comment1 = $commentRepository->find($comment1->id);
     $comment2 = $commentRepository->find($comment2->id);
     // Persist is cascaded
     $this->assertNotNull($blogPost);
     $this->assertCount(2, $blogPost->comments);
     $this->assertNotNull($comment1);
     $this->assertNotNull($comment2);
     unset($blogPost->comments[0]);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $blogPostRepository->find($blogPost->id);
     $comment1 = $commentRepository->find($comment1->id);
     $comment2 = $commentRepository->find($comment2->id);
     // Non-owning side of mappedBy reference is immutable
     $this->assertNotNull($blogPost);
     $this->assertCount(2, $blogPost->comments);
     $this->assertNotNull($comment1);
     $this->assertNotNull($comment2);
     $this->dm->remove($blogPost);
     $this->dm->flush();
     $this->dm->clear();
     $blogPost = $blogPostRepository->find($blogPost->id);
     $comment1 = $commentRepository->find($comment1->id);
     $comment2 = $commentRepository->find($comment2->id);
     // Remove cascades to referenced documents
     $this->assertNull($blogPost);
     $this->assertNull($comment1);
     $this->assertNull($comment2);
 }