ManyToManySelfReferentialAssociationTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  4. use Doctrine\ORM\Mapping\AssociationMapping;
  5. use Doctrine\ORM\Mapping\ClassMetadata;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a self referential many-to-many association mapping (from a model to the same model, without inheritance).
  9. * For simplicity the relation duplicates entries in the association table
  10. * to remain simmetrical.
  11. */
  12. class ManyToManySelfReferentialAssociationTest extends AbstractManyToManyAssociationTestCase
  13. {
  14. protected $_firstField = 'product_id';
  15. protected $_secondField = 'related_id';
  16. protected $_table = 'ecommerce_products_related';
  17. private $firstProduct;
  18. private $secondProduct;
  19. private $firstRelated;
  20. private $secondRelated;
  21. protected function setUp()
  22. {
  23. $this->useModelSet('ecommerce');
  24. parent::setUp();
  25. $this->firstProduct = new ECommerceProduct();
  26. $this->secondProduct = new ECommerceProduct();
  27. $this->firstRelated = new ECommerceProduct();
  28. $this->firstRelated->setName("Business");
  29. $this->secondRelated = new ECommerceProduct();
  30. $this->secondRelated->setName("Home");
  31. }
  32. public function testSavesAManyToManyAssociationWithCascadeSaveSet()
  33. {
  34. $this->firstProduct->addRelated($this->firstRelated);
  35. $this->firstProduct->addRelated($this->secondRelated);
  36. $this->_em->persist($this->firstProduct);
  37. $this->_em->flush();
  38. $this->assertForeignKeysContain($this->firstProduct->getId(),
  39. $this->firstRelated->getId());
  40. $this->assertForeignKeysContain($this->firstProduct->getId(),
  41. $this->secondRelated->getId());
  42. }
  43. public function testRemovesAManyToManyAssociation()
  44. {
  45. $this->firstProduct->addRelated($this->firstRelated);
  46. $this->firstProduct->addRelated($this->secondRelated);
  47. $this->_em->persist($this->firstProduct);
  48. $this->firstProduct->removeRelated($this->firstRelated);
  49. $this->_em->flush();
  50. $this->assertForeignKeysNotContain($this->firstProduct->getId(),
  51. $this->firstRelated->getId());
  52. $this->assertForeignKeysContain($this->firstProduct->getId(),
  53. $this->secondRelated->getId());
  54. }
  55. public function testEagerLoadsOwningSide()
  56. {
  57. $this->_createLoadingFixture();
  58. $products = $this->_findProducts();
  59. $this->assertLoadingOfOwningSide($products);
  60. }
  61. public function testLazyLoadsOwningSide()
  62. {
  63. $this->_createLoadingFixture();
  64. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
  65. $metadata->associationMappings['related']['fetch'] = ClassMetadata::FETCH_LAZY;
  66. $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
  67. $products = $query->getResult();
  68. $this->assertLoadingOfOwningSide($products);
  69. }
  70. public function assertLoadingOfOwningSide($products)
  71. {
  72. list ($firstProduct, $secondProduct) = $products;
  73. $this->assertEquals(2, count($firstProduct->getRelated()));
  74. $this->assertEquals(2, count($secondProduct->getRelated()));
  75. $categories = $firstProduct->getRelated();
  76. $firstRelatedBy = $categories[0]->getRelated();
  77. $secondRelatedBy = $categories[1]->getRelated();
  78. $this->assertEquals(2, count($firstRelatedBy));
  79. $this->assertEquals(2, count($secondRelatedBy));
  80. $this->assertTrue($firstRelatedBy[0] instanceof ECommerceProduct);
  81. $this->assertTrue($firstRelatedBy[1] instanceof ECommerceProduct);
  82. $this->assertTrue($secondRelatedBy[0] instanceof ECommerceProduct);
  83. $this->assertTrue($secondRelatedBy[1] instanceof ECommerceProduct);
  84. $this->assertCollectionEquals($firstRelatedBy, $secondRelatedBy);
  85. }
  86. protected function _createLoadingFixture()
  87. {
  88. $this->firstProduct->addRelated($this->firstRelated);
  89. $this->firstProduct->addRelated($this->secondRelated);
  90. $this->secondProduct->addRelated($this->firstRelated);
  91. $this->secondProduct->addRelated($this->secondRelated);
  92. $this->_em->persist($this->firstProduct);
  93. $this->_em->persist($this->secondProduct);
  94. $this->_em->flush();
  95. $this->_em->clear();
  96. }
  97. protected function _findProducts()
  98. {
  99. $query = $this->_em->createQuery('SELECT p, r FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.related r ORDER BY p.id, r.id');
  100. return $query->getResult();
  101. }
  102. }