DDC735Test.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. class DDC735Test extends \Doctrine\Tests\OrmFunctionalTestCase
  6. {
  7. protected function setUp()
  8. {
  9. parent::setUp();
  10. try {
  11. $this->_schemaTool->createSchema(array(
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Product'),
  13. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Review')
  14. ));
  15. } catch(\Exception $e) {
  16. }
  17. }
  18. public function testRemoveElement_AppliesOrphanRemoval()
  19. {
  20. // Create a product and its first review
  21. $product = new DDC735Product;
  22. $review = new DDC735Review($product);
  23. // Persist and flush
  24. $this->_em->persist($product);
  25. $this->_em->flush();
  26. // Now you see it
  27. $this->assertEquals(1, count($product->getReviews()));
  28. // Remove the review
  29. $reviewId = $review->getId();
  30. $product->removeReview($review);
  31. $this->_em->flush();
  32. // Now you don't
  33. $this->assertEquals(0, count($product->getReviews()), 'count($reviews) should be 0 after removing its only Review');
  34. // Refresh
  35. $this->_em->refresh($product);
  36. // It should still be 0
  37. $this->assertEquals(0, count($product->getReviews()), 'count($reviews) should still be 0 after the refresh');
  38. // Review should also not be available anymore
  39. $this->assertNull($this->_em->find(__NAMESPACE__.'\DDC735Review', $reviewId));
  40. }
  41. }
  42. /**
  43. * @Entity
  44. */
  45. class DDC735Product
  46. {
  47. /**
  48. * @Id @Column(type="integer") @GeneratedValue
  49. */
  50. protected $id;
  51. /**
  52. * @OneToMany(
  53. * targetEntity="DDC735Review",
  54. * mappedBy="product",
  55. * cascade={"persist"},
  56. * orphanRemoval=true
  57. * )
  58. */
  59. protected $reviews;
  60. public function __construct()
  61. {
  62. $this->reviews = new ArrayCollection;
  63. }
  64. public function getReviews()
  65. {
  66. return $this->reviews;
  67. }
  68. public function addReview(DDC735Review $review)
  69. {
  70. $this->reviews->add($review);
  71. }
  72. public function removeReview(DDC735Review $review)
  73. {
  74. $this->reviews->removeElement($review);
  75. }
  76. }
  77. /**
  78. * @Entity
  79. */
  80. class DDC735Review
  81. {
  82. /**
  83. * @Id @Column(type="integer") @GeneratedValue
  84. */
  85. protected $id;
  86. /**
  87. * @ManyToOne(targetEntity="DDC735Product", inversedBy="reviews")
  88. */
  89. protected $product;
  90. public function __construct(DDC735Product $product)
  91. {
  92. $this->product = $product;
  93. $product->addReview($this);
  94. }
  95. public function getId()
  96. {
  97. return $this->id;
  98. }
  99. }