DDC881Test.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC881Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. try {
  10. $this->_schemaTool->createSchema(array(
  11. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881User'),
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonenumber'),
  13. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonecall'),
  14. ));
  15. } catch (\Exception $e) {
  16. }
  17. }
  18. /**
  19. * @group DDC-117
  20. * @group DDC-881
  21. */
  22. public function testIssue()
  23. {
  24. /* Create two test users: albert and alfons */
  25. $albert = new DDC881User;
  26. $albert->setName("albert");
  27. $this->_em->persist($albert);
  28. $alfons = new DDC881User;
  29. $alfons->setName("alfons");
  30. $this->_em->persist($alfons);
  31. $this->_em->flush();
  32. /* Assign two phone numbers to each user */
  33. $phoneAlbert1 = new DDC881PhoneNumber();
  34. $phoneAlbert1->setUser($albert);
  35. $phoneAlbert1->setId(1);
  36. $phoneAlbert1->setPhoneNumber("albert home: 012345");
  37. $this->_em->persist($phoneAlbert1);
  38. $phoneAlbert2 = new DDC881PhoneNumber();
  39. $phoneAlbert2->setUser($albert);
  40. $phoneAlbert2->setId(2);
  41. $phoneAlbert2->setPhoneNumber("albert mobile: 67890");
  42. $this->_em->persist($phoneAlbert2);
  43. $phoneAlfons1 = new DDC881PhoneNumber();
  44. $phoneAlfons1->setId(1);
  45. $phoneAlfons1->setUser($alfons);
  46. $phoneAlfons1->setPhoneNumber("alfons home: 012345");
  47. $this->_em->persist($phoneAlfons1);
  48. $phoneAlfons2 = new DDC881PhoneNumber();
  49. $phoneAlfons2->setId(2);
  50. $phoneAlfons2->setUser($alfons);
  51. $phoneAlfons2->setPhoneNumber("alfons mobile: 67890");
  52. $this->_em->persist($phoneAlfons2);
  53. /* We call alfons and albert once on their mobile numbers */
  54. $call1 = new DDC881PhoneCall();
  55. $call1->setPhoneNumber($phoneAlfons2);
  56. $this->_em->persist($call1);
  57. $call2 = new DDC881PhoneCall();
  58. $call2->setPhoneNumber($phoneAlbert2);
  59. $this->_em->persist($call2);
  60. $this->_em->flush();
  61. $this->_em->clear();
  62. // fetch-join that foreign-key/primary-key entity association
  63. $dql = "SELECT c, p FROM " . __NAMESPACE__ . "\DDC881PhoneCall c JOIN c.phonenumber p";
  64. $calls = $this->_em->createQuery($dql)->getResult();
  65. $this->assertEquals(2, count($calls));
  66. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[0]->getPhoneNumber());
  67. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[1]->getPhoneNumber());
  68. $dql = "SELECT p, c FROM " . __NAMESPACE__ . "\DDC881PhoneNumber p JOIN p.calls c";
  69. $numbers = $this->_em->createQuery($dql)->getResult();
  70. $this->assertEquals(2, count($numbers));
  71. $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $numbers[0]->getCalls());
  72. $this->assertTrue($numbers[0]->getCalls()->isInitialized());
  73. }
  74. }
  75. /**
  76. * @Entity
  77. */
  78. class DDC881User
  79. {
  80. /**
  81. * @Id
  82. * @Column(type="integer")
  83. * @GeneratedValue(strategy="AUTO")
  84. */
  85. private $id;
  86. /**
  87. * @Column(type="string")
  88. */
  89. private $name;
  90. /**
  91. * @OneToMany(targetEntity="DDC881PhoneNumber",mappedBy="id")
  92. */
  93. private $phoneNumbers;
  94. public function getName()
  95. {
  96. return $this->name;
  97. }
  98. public function setName($name)
  99. {
  100. $this->name = $name;
  101. }
  102. }
  103. /**
  104. * @Entity
  105. */
  106. class DDC881PhoneNumber
  107. {
  108. /**
  109. * @Id
  110. * @Column(type="integer")
  111. */
  112. private $id;
  113. /**
  114. * @Id
  115. * @ManyToOne(targetEntity="DDC881User",cascade={"all"})
  116. */
  117. private $user;
  118. /**
  119. * @Column(type="string")
  120. */
  121. private $phonenumber;
  122. /**
  123. * @OneToMany(targetEntity="DDC881PhoneCall", mappedBy="phonenumber")
  124. */
  125. private $calls;
  126. public function __construct()
  127. {
  128. $this->calls = new \Doctrine\Common\Collections\ArrayCollection();
  129. }
  130. public function setId($id)
  131. {
  132. $this->id = $id;
  133. }
  134. public function setUser(DDC881User $user)
  135. {
  136. $this->user = $user;
  137. }
  138. public function setPhoneNumber($phoneNumber)
  139. {
  140. $this->phonenumber = $phoneNumber;
  141. }
  142. public function getCalls()
  143. {
  144. return $this->calls;
  145. }
  146. }
  147. /**
  148. * @Entity
  149. */
  150. class DDC881PhoneCall
  151. {
  152. /**
  153. * @Id
  154. * @Column(type="integer")
  155. * @GeneratedValue(strategy="AUTO")
  156. */
  157. private $id;
  158. /**
  159. * @ManyToOne(targetEntity="DDC881PhoneNumber", inversedBy="calls", cascade={"all"})
  160. * @JoinColumns({
  161. * @JoinColumn(name="phonenumber_id", referencedColumnName="id"),
  162. * @JoinColumn(name="user_id", referencedColumnName="user_id")
  163. * })
  164. */
  165. private $phonenumber;
  166. /**
  167. * @Column(type="string",nullable=true)
  168. */
  169. private $callDate;
  170. public function setPhoneNumber(DDC881PhoneNumber $phoneNumber)
  171. {
  172. $this->phonenumber = $phoneNumber;
  173. }
  174. public function getPhoneNumber()
  175. {
  176. return $this->phonenumber;
  177. }
  178. }