OneToOneEagerLoadingTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\TrainOwner'),
  19. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Waggon'),
  20. ));
  21. } catch(\Exception $e) {}
  22. }
  23. public function testEagerLoadOneToOneOwningSide()
  24. {
  25. $train = new Train(new TrainOwner("Alexander"));
  26. $driver = new TrainDriver("Benjamin");
  27. $waggon = new Waggon();
  28. $train->setDriver($driver);
  29. $train->addWaggon($waggon);
  30. $this->_em->persist($train); // cascades
  31. $this->_em->flush();
  32. $this->_em->clear();
  33. $sqlCount = count($this->_sqlLoggerStack->queries);
  34. $train = $this->_em->find(get_class($train), $train->id);
  35. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
  36. $this->assertEquals("Benjamin", $train->driver->name);
  37. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  38. }
  39. public function testEagerLoadOneToOneNullOwningSide()
  40. {
  41. $train = new Train(new TrainOwner("Alexander"));
  42. $this->_em->persist($train); // cascades
  43. $this->_em->flush();
  44. $this->_em->clear();
  45. $sqlCount = count($this->_sqlLoggerStack->queries);
  46. $train = $this->_em->find(get_class($train), $train->id);
  47. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
  48. $this->assertNull($train->driver);
  49. $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
  50. }
  51. public function testEagerLoadOneToOneInverseSide()
  52. {
  53. $owner = new TrainOwner("Alexander");
  54. $train = new Train($owner);
  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($owner), $owner->id);
  60. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $owner->train);
  61. $this->assertNotNull($owner->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(new TrainOwner("Alexander"));
  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. public function testEagerLoadWithNullableColumnsGeneratesLeftJoinOnBothSides()
  90. {
  91. $train = new Train(new TrainOwner("Alexander"));
  92. $driver = new TrainDriver("Benjamin");
  93. $train->setDriver($driver);
  94. $this->_em->persist($train);
  95. $this->_em->flush();
  96. $this->_em->clear();
  97. $train = $this->_em->find(get_class($train), $train->id);
  98. $this->assertSQLEquals(
  99. "SELECT t0.id AS id1, t0.driver_id AS driver_id2, t3.id AS id4, t3.name AS name5, t0.owner_id AS owner_id6, t7.id AS id8, t7.name AS name9 FROM Train t0 LEFT JOIN TrainDriver t3 ON t0.driver_id = t3.id INNER JOIN TrainOwner t7 ON t0.owner_id = t7.id WHERE t0.id = ?",
  100. $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
  101. );
  102. $this->_em->clear();
  103. $driver = $this->_em->find(get_class($driver), $driver->id);
  104. $this->assertSQLEquals(
  105. "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?)",
  106. $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
  107. );
  108. }
  109. public function testEagerLoadWithNonNullableColumnsGeneratesInnerJoinOnOwningSide()
  110. {
  111. $waggon = new Waggon();
  112. // It should have a train
  113. $train = new Train(new TrainOwner("Alexander"));
  114. $train->addWaggon($waggon);
  115. $this->_em->persist($train);
  116. $this->_em->flush();
  117. $this->_em->clear();
  118. $waggon = $this->_em->find(get_class($waggon), $waggon->id);
  119. // The last query is the eager loading of the owner of the train
  120. $this->assertSQLEquals(
  121. "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?)",
  122. $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
  123. );
  124. // The one before is the fetching of the waggon and train
  125. $this->assertSQLEquals(
  126. "SELECT t0.id AS id1, t0.train_id AS train_id2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM Waggon t0 INNER JOIN Train t3 ON t0.train_id = t3.id WHERE t0.id = ?",
  127. $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery - 1]['sql']
  128. );
  129. }
  130. public function testEagerLoadWithNonNullableColumnsGeneratesLeftJoinOnNonOwningSide()
  131. {
  132. $owner = new TrainOwner('Alexander');
  133. $train = new Train($owner);
  134. $this->_em->persist($train);
  135. $this->_em->flush();
  136. $this->_em->clear();
  137. $waggon = $this->_em->find(get_class($owner), $owner->id);
  138. $this->assertSQLEquals(
  139. "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id = ?",
  140. $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
  141. );
  142. }
  143. }
  144. /**
  145. * @Entity
  146. */
  147. class Train
  148. {
  149. /**
  150. * @id @column(type="integer") @generatedValue
  151. * @var int
  152. */
  153. public $id;
  154. /**
  155. * Owning side
  156. * @OneToOne(targetEntity="TrainDriver", inversedBy="train", fetch="EAGER", cascade={"persist"})
  157. * @JoinColumn(nullable=true)
  158. */
  159. public $driver;
  160. /**
  161. * Owning side
  162. * @OneToOne(targetEntity="TrainOwner", inversedBy="train", fetch="EAGER", cascade={"persist"})
  163. * @JoinColumn(nullable=false)
  164. */
  165. public $owner;
  166. /**
  167. * @oneToMany(targetEntity="Waggon", mappedBy="train", cascade={"persist"})
  168. */
  169. public $waggons;
  170. public function __construct(TrainOwner $owner)
  171. {
  172. $this->waggons = new \Doctrine\Common\Collections\ArrayCollection();
  173. $this->setOwner($owner);
  174. }
  175. public function setDriver(TrainDriver $driver)
  176. {
  177. $this->driver = $driver;
  178. $driver->setTrain($this);
  179. }
  180. public function setOwner(TrainOwner $owner)
  181. {
  182. $this->owner = $owner;
  183. $owner->setTrain($this);
  184. }
  185. public function addWaggon(Waggon $w)
  186. {
  187. $w->setTrain($this);
  188. $this->waggons[] = $w;
  189. }
  190. }
  191. /**
  192. * @Entity
  193. */
  194. class TrainDriver
  195. {
  196. /** @Id @Column(type="integer") @GeneratedValue */
  197. public $id;
  198. /** @column(type="string") */
  199. public $name;
  200. /**
  201. * Inverse side
  202. * @OneToOne(targetEntity="Train", mappedBy="driver", fetch="EAGER")
  203. */
  204. public $train;
  205. public function __construct($name)
  206. {
  207. $this->name = $name;
  208. }
  209. public function setTrain(Train $t)
  210. {
  211. $this->train = $t;
  212. }
  213. }
  214. /**
  215. * @Entity
  216. */
  217. class TrainOwner
  218. {
  219. /** @Id @Column(type="integer") @GeneratedValue */
  220. public $id;
  221. /** @column(type="string") */
  222. public $name;
  223. /**
  224. * Inverse side
  225. * @OneToOne(targetEntity="Train", mappedBy="owner", fetch="EAGER")
  226. */
  227. public $train;
  228. public function __construct($name)
  229. {
  230. $this->name = $name;
  231. }
  232. public function setTrain(Train $t)
  233. {
  234. $this->train = $t;
  235. }
  236. }
  237. /**
  238. * @Entity
  239. */
  240. class Waggon
  241. {
  242. /** @id @generatedValue @column(type="integer") */
  243. public $id;
  244. /**
  245. * @ManyToOne(targetEntity="Train", inversedBy="waggons", fetch="EAGER")
  246. * @JoinColumn(nullable=false)
  247. */
  248. public $train;
  249. public function setTrain($train)
  250. {
  251. $this->train = $train;
  252. }
  253. }