DDC1430Test.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. /**
  5. * @group DDC-1430
  6. */
  7. class DDC1430Test extends \Doctrine\Tests\OrmFunctionalTestCase
  8. {
  9. protected function setUp()
  10. {
  11. parent::setUp();
  12. try {
  13. $this->_schemaTool->createSchema(array(
  14. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430Order'),
  15. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430OrderProduct'),
  16. ));
  17. $this->loadFixtures();
  18. } catch (\Exception $exc) {
  19. }
  20. }
  21. public function testOrderByFields()
  22. {
  23. $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
  24. $builder = $repository->createQueryBuilder('o');
  25. $query = $builder->select('o.id, o.date, COUNT(p.id) AS p_count')
  26. ->leftJoin('o.products', 'p')
  27. ->groupBy('o.id, o.date')
  28. ->orderBy('o.id')
  29. ->getQuery();
  30. $this->assertEquals('SELECT o.id, o.date, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date ORDER BY o.id ASC', $query->getDQL());
  31. $this->assertSQLEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, COUNT(d1_.id) AS sclr2 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at ORDER BY d0_.order_id ASC', $query->getSQL());
  32. $result = $query->getResult();
  33. $this->assertEquals(2, sizeof($result));
  34. $this->assertArrayHasKey('id', $result[0]);
  35. $this->assertArrayHasKey('id', $result[1]);
  36. $this->assertArrayHasKey('p_count', $result[0]);
  37. $this->assertArrayHasKey('p_count', $result[1]);
  38. $this->assertEquals(1, $result[0]['id']);
  39. $this->assertEquals(2, $result[1]['id']);
  40. $this->assertEquals(2, $result[0]['p_count']);
  41. $this->assertEquals(3, $result[1]['p_count']);
  42. }
  43. public function testOrderByAllObjectFields()
  44. {
  45. $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
  46. $builder = $repository->createQueryBuilder('o');
  47. $query = $builder->select('o, COUNT(p.id) AS p_count')
  48. ->leftJoin('o.products', 'p')
  49. ->groupBy('o.id, o.date, o.status')
  50. ->orderBy('o.id')
  51. ->getQuery();
  52. $this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date, o.status ORDER BY o.id ASC', $query->getDQL());
  53. $this->assertSQLEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
  54. $result = $query->getResult();
  55. $this->assertEquals(2, sizeof($result));
  56. $this->assertTrue($result[0][0] instanceof DDC1430Order);
  57. $this->assertTrue($result[1][0] instanceof DDC1430Order);
  58. $this->assertEquals($result[0][0]->getId(), 1);
  59. $this->assertEquals($result[1][0]->getId(), 2);
  60. $this->assertEquals($result[0]['p_count'], 2);
  61. $this->assertEquals($result[1]['p_count'], 3);
  62. }
  63. public function testTicket()
  64. {
  65. $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
  66. $builder = $repository->createQueryBuilder('o');
  67. $query = $builder->select('o, COUNT(p.id) AS p_count')
  68. ->leftJoin('o.products', 'p')
  69. ->groupBy('o')
  70. ->orderBy('o.id')
  71. ->getQuery();
  72. $this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o ORDER BY o.id ASC', $query->getDQL());
  73. $this->assertSQLEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
  74. $result = $query->getResult();
  75. $this->assertEquals(2, sizeof($result));
  76. $this->assertTrue($result[0][0] instanceof DDC1430Order);
  77. $this->assertTrue($result[1][0] instanceof DDC1430Order);
  78. $this->assertEquals($result[0][0]->getId(), 1);
  79. $this->assertEquals($result[1][0]->getId(), 2);
  80. $this->assertEquals($result[0]['p_count'], 2);
  81. $this->assertEquals($result[1]['p_count'], 3);
  82. }
  83. public function loadFixtures()
  84. {
  85. $o1 = new DDC1430Order('NEW');
  86. $o2 = new DDC1430Order('OK');
  87. $o1->addProduct(new DDC1430OrderProduct(1.1));
  88. $o1->addProduct(new DDC1430OrderProduct(1.2));
  89. $o2->addProduct(new DDC1430OrderProduct(2.1));
  90. $o2->addProduct(new DDC1430OrderProduct(2.2));
  91. $o2->addProduct(new DDC1430OrderProduct(2.3));
  92. $this->_em->persist($o1);
  93. $this->_em->persist($o2);
  94. $this->_em->flush();
  95. }
  96. }
  97. /**
  98. * @Entity
  99. */
  100. class DDC1430Order
  101. {
  102. /**
  103. * @Id
  104. * @Column(name="order_id", type="integer")
  105. * @GeneratedValue()
  106. */
  107. protected $id;
  108. /**
  109. * @Column(name="created_at", type="datetime")
  110. */
  111. private $date;
  112. /**
  113. * @Column(name="order_status", type="string")
  114. */
  115. private $status;
  116. /**
  117. * @OneToMany(targetEntity="DDC1430OrderProduct", mappedBy="order", cascade={"persist", "remove"})
  118. *
  119. * @var \Doctrine\Common\Collections\ArrayCollection $products
  120. */
  121. private $products;
  122. /**
  123. * @return integer
  124. */
  125. public function getId()
  126. {
  127. return $this->id;
  128. }
  129. public function __construct($status)
  130. {
  131. $this->status = $status;
  132. $this->date = new \DateTime();
  133. $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  134. }
  135. /**
  136. * @return \DateTime
  137. */
  138. public function getDate()
  139. {
  140. return $this->date;
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function getStatus()
  146. {
  147. return $this->status;
  148. }
  149. /**
  150. * @param string $status
  151. */
  152. public function setStatus($status)
  153. {
  154. $this->status = $status;
  155. }
  156. /**
  157. * @return \Doctrine\Common\Collections\ArrayCollection
  158. */
  159. public function getProducts()
  160. {
  161. return $this->products;
  162. }
  163. /**
  164. * @param DDC1430OrderProduct $product
  165. */
  166. public function addProduct(DDC1430OrderProduct $product)
  167. {
  168. $product->setOrder($this);
  169. $this->products->add($product);
  170. }
  171. }
  172. /**
  173. * @Entity
  174. */
  175. class DDC1430OrderProduct
  176. {
  177. /**
  178. * @Id
  179. * @Column(type="integer")
  180. * @GeneratedValue()
  181. */
  182. protected $id;
  183. /**
  184. * @var DDC1430Order $order
  185. *
  186. * @ManyToOne(targetEntity="DDC1430Order", inversedBy="products")
  187. * @JoinColumn(name="order_id", referencedColumnName="order_id", nullable = false)
  188. */
  189. private $order;
  190. /**
  191. * @column(type="float")
  192. */
  193. private $value;
  194. /**
  195. * @param float $value
  196. */
  197. public function __construct($value)
  198. {
  199. $this->value = $value;
  200. }
  201. /**
  202. * @return integer
  203. */
  204. public function getId()
  205. {
  206. return $this->id;
  207. }
  208. /**
  209. * @return DDC1430Order
  210. */
  211. public function getOrder()
  212. {
  213. return $this->order;
  214. }
  215. /**
  216. * @param DDC1430Order $order
  217. */
  218. public function setOrder(DDC1430Order $order)
  219. {
  220. $this->order = $order;
  221. }
  222. /**
  223. * @return float
  224. */
  225. public function getValue()
  226. {
  227. return $this->value;
  228. }
  229. /**
  230. * @param float $value
  231. */
  232. public function setValue($value)
  233. {
  234. $this->value = $value;
  235. }
  236. }