DDC618Test.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use DateTime;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. /**
  6. * @group DDC-618
  7. */
  8. class DDC618Test extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. protected function setUp()
  11. {
  12. parent::setUp();
  13. try {
  14. $this->_schemaTool->createSchema(array(
  15. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Author'),
  16. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Book')
  17. ));
  18. // Create author 10/Joe with two books 22/JoeA and 20/JoeB
  19. $author = new DDC618Author();
  20. $author->id = 10;
  21. $author->name = 'Joe';
  22. $this->_em->persist($author);
  23. // Create author 11/Alice with two books 21/AliceA and 23/AliceB
  24. $author = new DDC618Author();
  25. $author->id = 11;
  26. $author->name = 'Alice';
  27. $author->addBook('In Wonderland');
  28. $author->addBook('Reloaded');
  29. $author->addBook('Test');
  30. $this->_em->persist($author);
  31. $this->_em->flush();
  32. $this->_em->clear();
  33. } catch(\Exception $e) {
  34. }
  35. }
  36. public function testIndexByHydrateObject()
  37. {
  38. $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
  39. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
  40. $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
  41. $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
  42. $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
  43. $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
  44. }
  45. public function testIndexByHydrateArray()
  46. {
  47. $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
  48. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  49. $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
  50. $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
  51. $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
  52. $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
  53. }
  54. /**
  55. * @group DDC-1018
  56. */
  57. public function testIndexByJoin()
  58. {
  59. $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A '.
  60. 'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
  61. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
  62. $this->assertEquals(3, count($result[0]->books)); // Alice, Joe doesnt appear because he has no books.
  63. $this->assertEquals('Alice', $result[0]->name);
  64. $this->assertTrue( isset($result[0]->books["In Wonderland"] ), "Indexing by title should have books by title.");
  65. $this->assertTrue( isset($result[0]->books["Reloaded"] ), "Indexing by title should have books by title.");
  66. $this->assertTrue( isset($result[0]->books["Test"] ), "Indexing by title should have books by title.");
  67. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  68. $this->assertEquals(3, count($result[0]['books'])); // Alice, Joe doesnt appear because he has no books.
  69. $this->assertEquals('Alice', $result[0]['name']);
  70. $this->assertTrue( isset($result[0]['books']["In Wonderland"] ), "Indexing by title should have books by title.");
  71. $this->assertTrue( isset($result[0]['books']["Reloaded"] ), "Indexing by title should have books by title.");
  72. $this->assertTrue( isset($result[0]['books']["Test"] ), "Indexing by title should have books by title.");
  73. }
  74. /**
  75. * @group DDC-1018
  76. */
  77. public function testIndexByToOneJoinSilentlyIgnored()
  78. {
  79. $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
  80. 'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
  81. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
  82. $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Book', $result[0]);
  83. $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', $result[0]->author);
  84. $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
  85. 'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
  86. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  87. $this->assertEquals("Alice", $result[0]['author']['name']);
  88. }
  89. /**
  90. * @group DDC-1018
  91. */
  92. public function testCombineIndexBy()
  93. {
  94. $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.id '.
  95. 'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
  96. $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
  97. $this->assertArrayHasKey(11, $result); // Alice
  98. $this->assertEquals(3, count($result[11]->books)); // Alice, Joe doesnt appear because he has no books.
  99. $this->assertEquals('Alice', $result[11]->name);
  100. $this->assertTrue( isset($result[11]->books["In Wonderland"] ), "Indexing by title should have books by title.");
  101. $this->assertTrue( isset($result[11]->books["Reloaded"] ), "Indexing by title should have books by title.");
  102. $this->assertTrue( isset($result[11]->books["Test"] ), "Indexing by title should have books by title.");
  103. }
  104. }
  105. /**
  106. * @Entity
  107. */
  108. class DDC618Author
  109. {
  110. /**
  111. * @Id
  112. * @Column(type="integer")
  113. */
  114. public $id;
  115. /** @Column(type="string") */
  116. public $name;
  117. /**
  118. * @OneToMany(targetEntity="DDC618Book", mappedBy="author", cascade={"persist"})
  119. */
  120. public $books;
  121. public function __construct()
  122. {
  123. $this->books = new \Doctrine\Common\Collections\ArrayCollection;
  124. }
  125. public function addBook($title)
  126. {
  127. $book = new DDC618Book($title, $this);
  128. $this->books[] = $book;
  129. }
  130. }
  131. /**
  132. * @Entity
  133. */
  134. class DDC618Book
  135. {
  136. /**
  137. * @Id @GeneratedValue
  138. * @Column(type="integer")
  139. */
  140. public $id;
  141. /** @column(type="string") */
  142. public $title;
  143. /** @ManyToOne(targetEntity="DDC618Author", inversedBy="books") */
  144. public $author;
  145. function __construct($title, $author)
  146. {
  147. $this->title = $title;
  148. $this->author = $author;
  149. }
  150. }