DDC440Test.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. try {
  10. $this->_schemaTool->createSchema(array(
  11. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone'),
  12. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\DDC440Client')
  13. ));
  14. } catch (\Exception $e) {
  15. // Swallow all exceptions. We do not test the schema tool here.
  16. }
  17. }
  18. /**
  19. * @group DDC-440
  20. */
  21. public function testOriginalEntityDataEmptyWhenProxyLoadedFromTwoAssociations()
  22. {
  23. /* The key of the problem is that the first phone is fetched via two association, main_phone and phones.
  24. *
  25. * You will notice that the original_entity_datas are not loaded for the first phone. (They are for the second)
  26. *
  27. * In the Client entity definition, if you define the main_phone relation after the phones relation, both assertions pass.
  28. * (for the sake or this test, I defined the main_phone relation before the phones relation)
  29. *
  30. */
  31. //Initialize some data
  32. $client = new DDC440Client;
  33. $client->setName('Client1');
  34. $phone = new DDC440Phone;
  35. $phone->setNumber('418 111-1111');
  36. $phone->setClient($client);
  37. $phone2 = new DDC440Phone;
  38. $phone2->setNumber('418 222-2222');
  39. $phone2->setClient($client);
  40. $client->setMainPhone($phone);
  41. $this->_em->persist($client);
  42. $this->_em->flush();
  43. $id = $client->getId();
  44. $this->_em->clear();
  45. $uw = $this->_em->getUnitOfWork();
  46. $client = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC440Client', $id);
  47. $clientPhones = $client->getPhones();
  48. $p1 = $clientPhones[0];
  49. $p2 = $clientPhones[1];
  50. // Test the first phone. The assertion actually failed because original entity data is not set properly.
  51. // This was because it is also set as MainPhone and that one is created as a proxy, not the
  52. // original object when the find on Client is called. However loading proxies did not work correctly.
  53. $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone', $p1);
  54. $originalData = $uw->getOriginalEntityData($p1);
  55. $this->assertEquals($phone->getNumber(), $originalData['number']);
  56. //If you comment out previous test, this one should pass
  57. $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone', $p2);
  58. $originalData = $uw->getOriginalEntityData($p2);
  59. $this->assertEquals($phone2->getNumber(), $originalData['number']);
  60. }
  61. }
  62. /**
  63. * @Entity
  64. * @Table(name="phone")
  65. */
  66. class DDC440Phone
  67. {
  68. /**
  69. * @Column(name="id", type="integer")
  70. * @Id
  71. * @GeneratedValue(strategy="AUTO")
  72. */
  73. protected $id;
  74. /**
  75. * @ManyToOne(targetEntity="DDC440Client",inversedBy="phones")
  76. * @JoinColumns({
  77. * @JoinColumn(name="client_id", referencedColumnName="id")
  78. * })
  79. */
  80. protected $client;
  81. /**
  82. * @Column(name="phonenumber", type="string")
  83. */
  84. protected $number;
  85. public function setNumber($value)
  86. {
  87. $this->number = $value;
  88. }
  89. public function getNumber()
  90. {
  91. return $this->number;
  92. }
  93. public function setClient(DDC440Client $value, $update_inverse=true)
  94. {
  95. $this->client = $value;
  96. if ($update_inverse) {
  97. $value->addPhone($this);
  98. }
  99. }
  100. public function getClient()
  101. {
  102. return $this->client;
  103. }
  104. public function getId()
  105. {
  106. return $this->id;
  107. }
  108. public function setId($value)
  109. {
  110. $this->id = $value;
  111. }
  112. }
  113. /**
  114. * @Entity
  115. * @Table(name="client")
  116. */
  117. class DDC440Client
  118. {
  119. /**
  120. * @Column(name="id", type="integer")
  121. * @Id
  122. * @GeneratedValue(strategy="AUTO")
  123. */
  124. protected $id;
  125. /**
  126. * @OneToOne(targetEntity="DDC440Phone", fetch="EAGER")
  127. * @JoinColumns({
  128. * @JoinColumn(name="main_phone_id", referencedColumnName="id",onDelete="SET NULL")
  129. * })
  130. */
  131. protected $main_phone;
  132. /**
  133. * @OneToMany(targetEntity="DDC440Phone", mappedBy="client", cascade={"persist", "remove"}, fetch="EAGER")
  134. * @orderBy({"number"="ASC"})
  135. */
  136. protected $phones;
  137. /**
  138. * @Column(name="name", type="string")
  139. */
  140. protected $name;
  141. public function __construct()
  142. {
  143. }
  144. public function setName($value)
  145. {
  146. $this->name = $value;
  147. }
  148. public function getName()
  149. {
  150. return $this->name;
  151. }
  152. public function addPhone(DDC440Phone $value)
  153. {
  154. $this->phones[] = $value;
  155. $value->setClient($this, false);
  156. }
  157. public function getPhones()
  158. {
  159. return $this->phones;
  160. }
  161. public function setMainPhone(DDC440Phone $value)
  162. {
  163. $this->main_phone = $value;
  164. }
  165. public function getMainPhone()
  166. {
  167. return $this->main_phone;
  168. }
  169. public function getId()
  170. {
  171. return $this->id;
  172. }
  173. public function setId($value)
  174. {
  175. $this->id = $value;
  176. }
  177. }