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()); }
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 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); }