DDC237Test.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC237Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. $this->_schemaTool->createSchema(array(
  10. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC237EntityX'),
  11. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC237EntityY'),
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC237EntityZ')
  13. ));
  14. }
  15. public function testUninitializedProxyIsInitializedOnFetchJoin()
  16. {
  17. $x = new DDC237EntityX;
  18. $y = new DDC237EntityY;
  19. $z = new DDC237EntityZ;
  20. $x->data = 'X';
  21. $y->data = 'Y';
  22. $z->data = 'Z';
  23. $x->y = $y;
  24. $z->y = $y;
  25. $this->_em->persist($x);
  26. $this->_em->persist($y);
  27. $this->_em->persist($z);
  28. $this->_em->flush();
  29. $this->_em->clear();
  30. $x2 = $this->_em->find(get_class($x), $x->id); // proxy injected for Y
  31. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $x2->y);
  32. $this->assertFalse($x2->y->__isInitialized__);
  33. // proxy for Y is in identity map
  34. $z2 = $this->_em->createQuery('select z,y from ' . get_class($z) . ' z join z.y y where z.id = ?1')
  35. ->setParameter(1, $z->id)
  36. ->getSingleResult();
  37. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $z2->y);
  38. $this->assertTrue($z2->y->__isInitialized__);
  39. $this->assertEquals('Y', $z2->y->data);
  40. $this->assertEquals($y->id, $z2->y->id);
  41. // since the Y is the same, the instance from the identity map is
  42. // used, even if it is a proxy.
  43. $this->assertNotSame($x, $x2);
  44. $this->assertNotSame($z, $z2);
  45. $this->assertSame($z2->y, $x2->y);
  46. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $z2->y);
  47. }
  48. }
  49. /**
  50. * @Entity @Table(name="ddc237_x")
  51. */
  52. class DDC237EntityX
  53. {
  54. /**
  55. * @Id @Column(type="integer") @GeneratedValue
  56. */
  57. public $id;
  58. /**
  59. * @Column(type="string")
  60. */
  61. public $data;
  62. /**
  63. * @OneToOne(targetEntity="DDC237EntityY")
  64. * @JoinColumn(name="y_id", referencedColumnName="id")
  65. */
  66. public $y;
  67. }
  68. /** @Entity @Table(name="ddc237_y") */
  69. class DDC237EntityY
  70. {
  71. /**
  72. * @Id @Column(type="integer") @GeneratedValue
  73. */
  74. public $id;
  75. /**
  76. * @Column(type="string")
  77. */
  78. public $data;
  79. }
  80. /** @Entity @Table(name="ddc237_z") */
  81. class DDC237EntityZ
  82. {
  83. /** @Id @Column(type="integer") @GeneratedValue */
  84. public $id;
  85. /** @Column(type="string") */
  86. public $data;
  87. /**
  88. * @OneToOne(targetEntity="DDC237EntityY")
  89. * @JoinColumn(name="y_id", referencedColumnName="id")
  90. */
  91. public $y;
  92. }