OneToOneEagerLoadingTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\UnitOfWork;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. /**
  6. * @group DDC-952
  7. */
  8. class OneToOneEagerLoadingTest extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. protected function setUp()
  11. {
  12. parent::setUp();
  13. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
  14. try {
  15. $schemaTool->createSchema(array(
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Train'),
  17. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\TrainDriver'),
  18. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Waggon'),
  19. ));
  20. } catch(\Exception $e) {}
  21. }
  22. public function testEagerLoadOneToOneOwningSide()
  23. {
  24. $train = new Train();
  25. $driver = new TrainDriver("Benjamin");
  26. $waggon = new Waggon();
  27. $train->setDriver($driver);
  28. $train->addWaggon($waggon);
  29. $this->_em->persist($train); // cascades
  30. $this->_em->flush();
  31. $this->_em->clear();
  32. $sqlCount = count($this->_sqlLoggerStack->queries);
  33. $train = $this->_em->find(get_class($train), $train->id);
  34. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
  35. $this->assertEquals("Benjamin", $train->driver->name);
  36. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  37. }
  38. public function testEagerLoadOneToOneNullOwningSide()
  39. {
  40. $train = new Train();
  41. $this->_em->persist($train); // cascades
  42. $this->_em->flush();
  43. $this->_em->clear();
  44. $sqlCount = count($this->_sqlLoggerStack->queries);
  45. $train = $this->_em->find(get_class($train), $train->id);
  46. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
  47. $this->assertNull($train->driver);
  48. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  49. }
  50. public function testEagerLoadOneToOneInverseSide()
  51. {
  52. $train = new Train();
  53. $driver = new TrainDriver("Benjamin");
  54. $train->setDriver($driver);
  55. $this->_em->persist($train); // cascades
  56. $this->_em->flush();
  57. $this->_em->clear();
  58. $sqlCount = count($this->_sqlLoggerStack->queries);
  59. $driver = $this->_em->find(get_class($driver), $driver->id);
  60. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $driver->train);
  61. $this->assertNotNull($driver->train);
  62. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  63. }
  64. public function testEagerLoadOneToOneNullInverseSide()
  65. {
  66. $driver = new TrainDriver("Dagny Taggert");
  67. $this->_em->persist($driver);
  68. $this->_em->flush();
  69. $this->_em->clear();
  70. $this->assertNull($driver->train);
  71. $sqlCount = count($this->_sqlLoggerStack->queries);
  72. $driver = $this->_em->find(get_class($driver), $driver->id);
  73. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $driver->train);
  74. $this->assertNull($driver->train);
  75. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  76. }
  77. public function testEagerLoadManyToOne()
  78. {
  79. $train = new Train();
  80. $waggon = new Waggon();
  81. $train->addWaggon($waggon);
  82. $this->_em->persist($train); // cascades
  83. $this->_em->flush();
  84. $this->_em->clear();
  85. $waggon = $this->_em->find(get_class($waggon), $waggon->id);
  86. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $waggon->train);
  87. $this->assertNotNull($waggon->train);
  88. }
  89. }
  90. /**
  91. * @Entity
  92. */
  93. class Train
  94. {
  95. /**
  96. * @id @column(type="integer") @generatedValue
  97. * @var int
  98. */
  99. public $id;
  100. /**
  101. * Owning side
  102. * @OneToOne(targetEntity="TrainDriver", inversedBy="train", fetch="EAGER", cascade={"persist"})
  103. */
  104. public $driver;
  105. /**
  106. * @oneToMany(targetEntity="Waggon", mappedBy="train", cascade={"persist"})
  107. */
  108. public $waggons;
  109. public function __construct()
  110. {
  111. $this->waggons = new \Doctrine\Common\Collections\ArrayCollection();
  112. }
  113. public function setDriver(TrainDriver $driver)
  114. {
  115. $this->driver = $driver;
  116. $driver->setTrain($this);
  117. }
  118. public function addWaggon(Waggon $w)
  119. {
  120. $w->setTrain($this);
  121. $this->waggons[] = $w;
  122. }
  123. }
  124. /**
  125. * @Entity
  126. */
  127. class TrainDriver
  128. {
  129. /** @Id @Column(type="integer") @GeneratedValue */
  130. public $id;
  131. /** @column(type="string") */
  132. public $name;
  133. /**
  134. * Inverse side
  135. * @OneToOne(targetEntity="Train", mappedBy="driver", fetch="EAGER")
  136. */
  137. public $train;
  138. public function __construct($name)
  139. {
  140. $this->name = $name;
  141. }
  142. public function setTrain(Train $t)
  143. {
  144. $this->train = $t;
  145. }
  146. }
  147. /**
  148. * @Entity
  149. */
  150. class Waggon
  151. {
  152. /** @id @generatedValue @column(type="integer") */
  153. public $id;
  154. /** @ManyToOne(targetEntity="Train", inversedBy="waggons", fetch="EAGER") */
  155. public $train;
  156. public function setTrain($train)
  157. {
  158. $this->train = $train;
  159. }
  160. }