DDC599Test.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC599Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  10. try {
  11. $this->_schemaTool->createSchema(array(
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Item'),
  13. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Subitem'),
  14. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Child'),
  15. ));
  16. } catch (\Exception $ignored) {}
  17. }
  18. public function testCascadeRemoveOnInheritanceHierachy()
  19. {
  20. $item = new DDC599Subitem;
  21. $item->elem = "foo";
  22. $child = new DDC599Child;
  23. $child->parent = $item;
  24. $item->getChildren()->add($child);
  25. $this->_em->persist($item);
  26. $this->_em->persist($child);
  27. $this->_em->flush();
  28. $this->_em->clear();
  29. $item = $this->_em->find(__NAMESPACE__ . '\DDC599Item', $item->id);
  30. $this->_em->remove($item);
  31. $this->_em->flush(); // Should not fail
  32. $this->assertFalse($this->_em->contains($item));
  33. $children = $item->getChildren();
  34. $this->assertFalse($this->_em->contains($children[0]));
  35. $this->_em->clear();
  36. $item2 = new DDC599Subitem;
  37. $item2->elem = "bar";
  38. $this->_em->persist($item2);
  39. $this->_em->flush();
  40. $child2 = new DDC599Child;
  41. $child2->parent = $item2;
  42. $item2->getChildren()->add($child2);
  43. $this->_em->persist($child2);
  44. $this->_em->flush();
  45. $this->_em->remove($item2);
  46. $this->_em->flush(); // should not fail
  47. $this->assertFalse($this->_em->contains($item));
  48. $children = $item->getChildren();
  49. $this->assertFalse($this->_em->contains($children[0]));
  50. }
  51. public function testCascadeRemoveOnChildren()
  52. {
  53. $class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC599Subitem');
  54. $this->assertArrayHasKey('children', $class->associationMappings);
  55. $this->assertTrue($class->associationMappings['children']['isCascadeRemove']);
  56. }
  57. }
  58. /**
  59. * @Entity
  60. * @InheritanceType("SINGLE_TABLE")
  61. * @DiscriminatorColumn(name="type", type="integer")
  62. * @DiscriminatorMap({"0" = "DDC599Item", "1" = "DDC599Subitem"})
  63. */
  64. class DDC599Item
  65. {
  66. /**
  67. * @Id
  68. * @Column(type="integer")
  69. * @GeneratedValue(strategy="AUTO")
  70. */
  71. public $id;
  72. /**
  73. * @OneToMany(targetEntity="DDC599Child", mappedBy="parent", cascade={"remove"})
  74. */
  75. protected $children;
  76. public function __construct()
  77. {
  78. $this->children = new \Doctrine\Common\Collections\ArrayCollection;
  79. }
  80. public function getChildren()
  81. {
  82. return $this->children;
  83. }
  84. }
  85. /**
  86. * @Entity
  87. */
  88. class DDC599Subitem extends DDC599Item
  89. {
  90. /**
  91. * @Column(type="string")
  92. */
  93. public $elem;
  94. }
  95. /**
  96. * @Entity
  97. */
  98. class DDC599Child
  99. {
  100. /**
  101. * @Id
  102. * @Column(type="integer")
  103. * @GeneratedValue(strategy="AUTO")
  104. */
  105. public $id;
  106. /**
  107. * @ManyToOne(targetEntity="DDC599Item", inversedBy="children")
  108. * @JoinColumn(name="parentId", referencedColumnName="id")
  109. */
  110. public $parent;
  111. }