DDC729Test.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC729Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. public function setUp()
  7. {
  8. parent::setUp();
  9. try {
  10. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
  11. $schemaTool->createSchema(array(
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC729A'),
  13. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC729B'),
  14. ));
  15. } catch(\Exception $e) {
  16. }
  17. }
  18. public function testMergeManyToMany()
  19. {
  20. $a = new DDC729A();
  21. $b = new DDC729B();
  22. $a->related[] = $b;
  23. $this->_em->persist($a);
  24. $this->_em->persist($b);
  25. $this->_em->flush();
  26. $this->_em->clear();
  27. $aId = $a->id;
  28. $a = new DDC729A();
  29. $a->id = $aId;
  30. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $a->related);
  31. $a = $this->_em->merge($a);
  32. $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $a->related);
  33. $this->assertFalse($a->related->isInitialized(), "Collection should not be marked initialized.");
  34. $this->assertFalse($a->related->isDirty(), "Collection should not be marked as dirty.");
  35. $this->_em->flush();
  36. $this->_em->clear();
  37. $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
  38. $this->assertEquals(1, count($a->related));
  39. }
  40. public function testUnidirectionalMergeManyToMany()
  41. {
  42. $a = new DDC729A();
  43. $b1 = new DDC729B();
  44. $b2 = new DDC729B();
  45. $a->related[] = $b1;
  46. $this->_em->persist($a);
  47. $this->_em->persist($b1);
  48. $this->_em->persist($b2);
  49. $this->_em->flush();
  50. $this->_em->clear();
  51. $aId = $a->id;
  52. $a = new DDC729A();
  53. $a->id = $aId;
  54. $a = $this->_em->merge($a);
  55. $a->related->set(0, $this->_em->merge($b1));
  56. $a->related->set(1, $this->_em->merge($b2));
  57. $this->_em->flush();
  58. $this->_em->clear();
  59. $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
  60. $this->assertEquals(2, count($a->related));
  61. }
  62. public function testBidirectionalMergeManyToMany()
  63. {
  64. $a = new DDC729A();
  65. $b1 = new DDC729B();
  66. $b2 = new DDC729B();
  67. $a->related[] = $b1;
  68. $this->_em->persist($a);
  69. $this->_em->persist($b1);
  70. $this->_em->persist($b2);
  71. $this->_em->flush();
  72. $this->_em->clear();
  73. $aId = $a->id;
  74. $a = new DDC729A();
  75. $a->id = $aId;
  76. $a = $this->_em->merge($a);
  77. $a->related->set(0, $this->_em->merge($b1));
  78. $b1->related->set(0, $a);
  79. $a->related->set(1, $this->_em->merge($b2));
  80. $b2->related->set(0, $a);
  81. $this->_em->flush();
  82. $this->_em->clear();
  83. $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
  84. $this->assertEquals(2, count($a->related));
  85. }
  86. public function testBidirectionalMultiMergeManyToMany()
  87. {
  88. $a = new DDC729A();
  89. $b1 = new DDC729B();
  90. $b2 = new DDC729B();
  91. $a->related[] = $b1;
  92. $this->_em->persist($a);
  93. $this->_em->persist($b1);
  94. $this->_em->persist($b2);
  95. $this->_em->flush();
  96. $this->_em->clear();
  97. $aId = $a->id;
  98. $a = new DDC729A();
  99. $a->id = $aId;
  100. $a = $this->_em->merge($a);
  101. $a->related->set(0, $this->_em->merge($b1));
  102. $b1->related->set(0, $this->_em->merge($a));
  103. $a->related->set(1, $this->_em->merge($b2));
  104. $b2->related->set(0, $this->_em->merge($a));
  105. $this->_em->flush();
  106. $this->_em->clear();
  107. $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
  108. $this->assertEquals(2, count($a->related));
  109. }
  110. }
  111. /**
  112. * @Entity
  113. */
  114. class DDC729A
  115. {
  116. /** @Id @GeneratedValue @Column(type="integer") */
  117. public $id;
  118. /** @ManyToMany(targetEntity="DDC729B", inversedBy="related") */
  119. public $related;
  120. public function __construct()
  121. {
  122. $this->related = new \Doctrine\Common\Collections\ArrayCollection();
  123. }
  124. }
  125. /**
  126. * @Entity
  127. */
  128. class DDC729B
  129. {
  130. /** @Id @GeneratedValue @Column(type="integer") */
  131. public $id;
  132. /** @ManyToMany(targetEntity="DDC729B", mappedBy="related") */
  133. public $related;
  134. public function __construct()
  135. {
  136. $this->related = new \Doctrine\Common\Collections\ArrayCollection();
  137. }
  138. }