ManyToManyBidirectionalAssociationTest.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  4. use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
  5. use Doctrine\ORM\Mapping\AssociationMapping;
  6. use Doctrine\ORM\Query;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. /**
  9. * Tests a bidirectional many-to-many association mapping (without inheritance).
  10. * Owning side is ECommerceProduct, inverse side is ECommerceCategory.
  11. */
  12. class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
  13. {
  14. protected $_firstField = 'product_id';
  15. protected $_secondField = 'category_id';
  16. protected $_table = 'ecommerce_products_categories';
  17. private $firstProduct;
  18. private $secondProduct;
  19. private $firstCategory;
  20. private $secondCategory;
  21. protected function setUp()
  22. {
  23. $this->useModelSet('ecommerce');
  24. parent::setUp();
  25. $this->firstProduct = new ECommerceProduct();
  26. $this->firstProduct->setName("First Product");
  27. $this->secondProduct = new ECommerceProduct();
  28. $this->secondProduct->setName("Second Product");
  29. $this->firstCategory = new ECommerceCategory();
  30. $this->firstCategory->setName("Business");
  31. $this->secondCategory = new ECommerceCategory();
  32. $this->secondCategory->setName("Home");
  33. }
  34. public function testSavesAManyToManyAssociationWithCascadeSaveSet()
  35. {
  36. $this->firstProduct->addCategory($this->firstCategory);
  37. $this->firstProduct->addCategory($this->secondCategory);
  38. $this->_em->persist($this->firstProduct);
  39. $this->_em->flush();
  40. $this->assertForeignKeysContain($this->firstProduct->getId(), $this->firstCategory->getId());
  41. $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
  42. }
  43. public function testRemovesAManyToManyAssociation()
  44. {
  45. $this->firstProduct->addCategory($this->firstCategory);
  46. $this->firstProduct->addCategory($this->secondCategory);
  47. $this->_em->persist($this->firstProduct);
  48. $this->firstProduct->removeCategory($this->firstCategory);
  49. $this->_em->flush();
  50. $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->firstCategory->getId());
  51. $this->assertForeignKeysContain($this->firstProduct->getId(), $this->secondCategory->getId());
  52. $this->firstProduct->getCategories()->remove(1);
  53. $this->_em->flush();
  54. $this->assertForeignKeysNotContain($this->firstProduct->getId(), $this->secondCategory->getId());
  55. }
  56. public function testEagerLoadFromInverseSideAndLazyLoadFromOwningSide()
  57. {
  58. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  59. $this->_createLoadingFixture();
  60. $categories = $this->_findCategories();
  61. $this->assertLazyLoadFromOwningSide($categories);
  62. }
  63. public function testEagerLoadFromOwningSideAndLazyLoadFromInverseSide()
  64. {
  65. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  66. $this->_createLoadingFixture();
  67. $products = $this->_findProducts();
  68. $this->assertLazyLoadFromInverseSide($products);
  69. }
  70. private function _createLoadingFixture()
  71. {
  72. $this->firstProduct->addCategory($this->firstCategory);
  73. $this->firstProduct->addCategory($this->secondCategory);
  74. $this->secondProduct->addCategory($this->firstCategory);
  75. $this->secondProduct->addCategory($this->secondCategory);
  76. $this->_em->persist($this->firstProduct);
  77. $this->_em->persist($this->secondProduct);
  78. $this->_em->flush();
  79. $this->_em->clear();
  80. }
  81. protected function _findProducts()
  82. {
  83. $query = $this->_em->createQuery('SELECT p, c FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.categories c ORDER BY p.id, c.id');
  84. //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  85. $result = $query->getResult();
  86. $this->assertEquals(2, count($result));
  87. $cats1 = $result[0]->getCategories();
  88. $cats2 = $result[1]->getCategories();
  89. $this->assertTrue($cats1->isInitialized());
  90. $this->assertTrue($cats2->isInitialized());
  91. $this->assertFalse($cats1[0]->getProducts()->isInitialized());
  92. $this->assertFalse($cats2[0]->getProducts()->isInitialized());
  93. return $result;
  94. }
  95. protected function _findCategories()
  96. {
  97. $query = $this->_em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCategory c LEFT JOIN c.products p ORDER BY c.id, p.id');
  98. //$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  99. $result = $query->getResult();
  100. $this->assertEquals(2, count($result));
  101. $this->assertTrue($result[0] instanceof ECommerceCategory);
  102. $this->assertTrue($result[1] instanceof ECommerceCategory);
  103. $prods1 = $result[0]->getProducts();
  104. $prods2 = $result[1]->getProducts();
  105. $this->assertTrue($prods1->isInitialized());
  106. $this->assertTrue($prods2->isInitialized());
  107. $this->assertFalse($prods1[0]->getCategories()->isInitialized());
  108. $this->assertFalse($prods2[0]->getCategories()->isInitialized());
  109. return $result;
  110. }
  111. public function assertLazyLoadFromInverseSide($products)
  112. {
  113. list ($firstProduct, $secondProduct) = $products;
  114. $firstProductCategories = $firstProduct->getCategories();
  115. $secondProductCategories = $secondProduct->getCategories();
  116. $this->assertEquals(2, count($firstProductCategories));
  117. $this->assertEquals(2, count($secondProductCategories));
  118. $this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
  119. $this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);
  120. $firstCategoryProducts = $firstProductCategories[0]->getProducts();
  121. $secondCategoryProducts = $firstProductCategories[1]->getProducts();
  122. $this->assertFalse($firstCategoryProducts->isInitialized());
  123. $this->assertFalse($secondCategoryProducts->isInitialized());
  124. $this->assertEquals(0, $firstCategoryProducts->unwrap()->count());
  125. $this->assertEquals(0, $secondCategoryProducts->unwrap()->count());
  126. $this->assertEquals(2, count($firstCategoryProducts)); // lazy-load
  127. $this->assertTrue($firstCategoryProducts->isInitialized());
  128. $this->assertFalse($secondCategoryProducts->isInitialized());
  129. $this->assertEquals(2, count($secondCategoryProducts)); // lazy-load
  130. $this->assertTrue($secondCategoryProducts->isInitialized());
  131. $this->assertTrue($firstCategoryProducts[0] instanceof ECommerceProduct);
  132. $this->assertTrue($firstCategoryProducts[1] instanceof ECommerceProduct);
  133. $this->assertTrue($secondCategoryProducts[0] instanceof ECommerceProduct);
  134. $this->assertTrue($secondCategoryProducts[1] instanceof ECommerceProduct);
  135. $this->assertCollectionEquals($firstCategoryProducts, $secondCategoryProducts);
  136. }
  137. public function assertLazyLoadFromOwningSide($categories)
  138. {
  139. list ($firstCategory, $secondCategory) = $categories;
  140. $firstCategoryProducts = $firstCategory->getProducts();
  141. $secondCategoryProducts = $secondCategory->getProducts();
  142. $this->assertEquals(2, count($firstCategoryProducts));
  143. $this->assertEquals(2, count($secondCategoryProducts));
  144. $this->assertTrue($firstCategoryProducts[0] === $secondCategoryProducts[0]);
  145. $this->assertTrue($firstCategoryProducts[1] === $secondCategoryProducts[1]);
  146. $firstProductCategories = $firstCategoryProducts[0]->getCategories();
  147. $secondProductCategories = $firstCategoryProducts[1]->getCategories();
  148. $this->assertFalse($firstProductCategories->isInitialized());
  149. $this->assertFalse($secondProductCategories->isInitialized());
  150. $this->assertEquals(0, $firstProductCategories->unwrap()->count());
  151. $this->assertEquals(0, $secondProductCategories->unwrap()->count());
  152. $this->assertEquals(2, count($firstProductCategories)); // lazy-load
  153. $this->assertTrue($firstProductCategories->isInitialized());
  154. $this->assertFalse($secondProductCategories->isInitialized());
  155. $this->assertEquals(2, count($secondProductCategories)); // lazy-load
  156. $this->assertTrue($secondProductCategories->isInitialized());
  157. $this->assertTrue($firstProductCategories[0] instanceof ECommerceCategory);
  158. $this->assertTrue($firstProductCategories[1] instanceof ECommerceCategory);
  159. $this->assertTrue($secondProductCategories[0] instanceof ECommerceCategory);
  160. $this->assertTrue($secondProductCategories[1] instanceof ECommerceCategory);
  161. $this->assertCollectionEquals($firstProductCategories, $secondProductCategories);
  162. }
  163. }