OneToManyBidirectionalAssociationTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  4. use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
  5. use Doctrine\ORM\Mapping\AssociationMapping;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a bidirectional one-to-one association mapping (without inheritance).
  9. */
  10. class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. private $product;
  13. private $firstFeature;
  14. private $secondFeature;
  15. protected function setUp()
  16. {
  17. $this->useModelSet('ecommerce');
  18. parent::setUp();
  19. $this->product = new ECommerceProduct();
  20. $this->product->setName('Doctrine Cookbook');
  21. $this->firstFeature = new ECommerceFeature();
  22. $this->firstFeature->setDescription('Model writing tutorial');
  23. $this->secondFeature = new ECommerceFeature();
  24. $this->secondFeature->setDescription('Annotations examples');
  25. }
  26. public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
  27. $this->product->addFeature($this->firstFeature);
  28. $this->product->addFeature($this->secondFeature);
  29. $this->_em->persist($this->product);
  30. $this->_em->flush();
  31. $this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
  32. $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
  33. }
  34. public function testSavesAnEmptyCollection()
  35. {
  36. $this->_em->persist($this->product);
  37. $this->_em->flush();
  38. $this->assertEquals(0, count($this->product->getFeatures()));
  39. }
  40. public function testDoesNotSaveAnInverseSideSet() {
  41. $this->product->brokenAddFeature($this->firstFeature);
  42. $this->_em->persist($this->product);
  43. $this->_em->flush();
  44. $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
  45. }
  46. public function testRemovesOneToOneAssociation()
  47. {
  48. $this->product->addFeature($this->firstFeature);
  49. $this->product->addFeature($this->secondFeature);
  50. $this->_em->persist($this->product);
  51. $this->product->removeFeature($this->firstFeature);
  52. $this->_em->flush();
  53. $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
  54. $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
  55. }
  56. public function testEagerLoadsOneToManyAssociation()
  57. {
  58. $this->_createFixture();
  59. $query = $this->_em->createQuery('select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f');
  60. $result = $query->getResult();
  61. $product = $result[0];
  62. $features = $product->getFeatures();
  63. $this->assertTrue($features[0] instanceof ECommerceFeature);
  64. $this->assertFalse($features[0]->getProduct() instanceof \Doctrine\ORM\Proxy\Proxy);
  65. $this->assertSame($product, $features[0]->getProduct());
  66. $this->assertEquals('Model writing tutorial', $features[0]->getDescription());
  67. $this->assertTrue($features[1] instanceof ECommerceFeature);
  68. $this->assertSame($product, $features[1]->getProduct());
  69. $this->assertFalse($features[1]->getProduct() instanceof \Doctrine\ORM\Proxy\Proxy);
  70. $this->assertEquals('Annotations examples', $features[1]->getDescription());
  71. }
  72. public function testLazyLoadsObjectsOnTheOwningSide()
  73. {
  74. $this->_createFixture();
  75. $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
  76. $result = $query->getResult();
  77. $product = $result[0];
  78. $features = $product->getFeatures();
  79. $this->assertFalse($features->isInitialized());
  80. $this->assertTrue($features[0] instanceof ECommerceFeature);
  81. $this->assertTrue($features->isInitialized());
  82. $this->assertSame($product, $features[0]->getProduct());
  83. $this->assertEquals('Model writing tutorial', $features[0]->getDescription());
  84. $this->assertTrue($features[1] instanceof ECommerceFeature);
  85. $this->assertSame($product, $features[1]->getProduct());
  86. $this->assertEquals('Annotations examples', $features[1]->getDescription());
  87. }
  88. public function testLazyLoadsObjectsOnTheInverseSide()
  89. {
  90. $this->_createFixture();
  91. $query = $this->_em->createQuery('select f from Doctrine\Tests\Models\ECommerce\ECommerceFeature f');
  92. $features = $query->getResult();
  93. $product = $features[0]->getProduct();
  94. $this->assertTrue($product instanceof \Doctrine\ORM\Proxy\Proxy);
  95. $this->assertTrue($product instanceof ECommerceProduct);
  96. $this->assertFalse($product->__isInitialized__);
  97. $this->assertSame('Doctrine Cookbook', $product->getName());
  98. $this->assertTrue($product->__isInitialized__);
  99. }
  100. public function testLazyLoadsObjectsOnTheInverseSide2()
  101. {
  102. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  103. $this->_createFixture();
  104. $query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
  105. $features = $query->getResult();
  106. $product = $features[0]->getProduct();
  107. $this->assertFalse($product instanceof \Doctrine\ORM\Proxy\Proxy);
  108. $this->assertTrue($product instanceof ECommerceProduct);
  109. $this->assertSame('Doctrine Cookbook', $product->getName());
  110. $this->assertFalse($product->getFeatures()->isInitialized());
  111. // This would trigger lazy-load
  112. //$this->assertEquals(2, $product->getFeatures()->count());
  113. //$this->assertTrue($product->getFeatures()->contains($features[0]));
  114. //$this->assertTrue($product->getFeatures()->contains($features[1]));
  115. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
  116. }
  117. public function testJoinFromOwningSide()
  118. {
  119. $query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
  120. $features = $query->getResult();
  121. $this->assertEquals(0, count($features));
  122. }
  123. private function _createFixture()
  124. {
  125. $this->product->addFeature($this->firstFeature);
  126. $this->product->addFeature($this->secondFeature);
  127. $this->_em->persist($this->product);
  128. $this->_em->flush();
  129. $this->_em->clear();
  130. }
  131. public function assertFeatureForeignKeyIs($value, ECommerceFeature $feature) {
  132. $foreignKey = $this->_em->getConnection()->executeQuery(
  133. 'SELECT product_id FROM ecommerce_features WHERE id=?',
  134. array($feature->getId())
  135. )->fetchColumn();
  136. $this->assertEquals($value, $foreignKey);
  137. }
  138. }