DDC1654Test.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. /**
  4. * @group DDC-1654
  5. */
  6. class DDC1654Test extends \Doctrine\Tests\OrmFunctionalTestCase
  7. {
  8. public function setUp()
  9. {
  10. parent::setUp();
  11. $this->setUpEntitySchema(array(
  12. __NAMESPACE__ . '\\DDC1654Post',
  13. __NAMESPACE__ . '\\DDC1654Comment',
  14. ));
  15. }
  16. public function testManyToManyRemoveFromCollectionOrphanRemoval()
  17. {
  18. $post = new DDC1654Post();
  19. $post->comments[] = new DDC1654Comment();
  20. $post->comments[] = new DDC1654Comment();
  21. $this->_em->persist($post);
  22. $this->_em->flush();
  23. $post->comments->remove(0);
  24. $post->comments->remove(1);
  25. $this->_em->flush();
  26. $this->_em->clear();
  27. $comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll();
  28. $this->assertEquals(0, count($comments));
  29. }
  30. public function testManyToManyRemoveElementFromCollectionOrphanRemoval()
  31. {
  32. $post = new DDC1654Post();
  33. $post->comments[] = new DDC1654Comment();
  34. $post->comments[] = new DDC1654Comment();
  35. $this->_em->persist($post);
  36. $this->_em->flush();
  37. $post->comments->removeElement($post->comments[0]);
  38. $post->comments->removeElement($post->comments[1]);
  39. $this->_em->flush();
  40. $this->_em->clear();
  41. $comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll();
  42. $this->assertEquals(0, count($comments));
  43. }
  44. public function testManyToManyClearCollectionOrphanRemoval()
  45. {
  46. $post = new DDC1654Post();
  47. $post->comments[] = new DDC1654Comment();
  48. $post->comments[] = new DDC1654Comment();
  49. $this->_em->persist($post);
  50. $this->_em->flush();
  51. $post->comments->clear();
  52. $this->_em->flush();
  53. $this->_em->clear();
  54. $comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll();
  55. $this->assertEquals(0, count($comments));
  56. }
  57. }
  58. /**
  59. * @Entity
  60. */
  61. class DDC1654Post
  62. {
  63. /**
  64. * @Id @Column(type="integer") @GeneratedValue
  65. */
  66. public $id;
  67. /**
  68. * @ManyToMany(targetEntity="DDC1654Comment", orphanRemoval=true,
  69. * cascade={"persist"})
  70. */
  71. public $comments = array();
  72. }
  73. /**
  74. * @Entity
  75. */
  76. class DDC1654Comment
  77. {
  78. /**
  79. * @Id @Column(type="integer") @GeneratedValue
  80. */
  81. public $id;
  82. }