DDC1655Test.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. /**
  4. * @group DDC-1655
  5. * @group DDC-1640
  6. * @group DDC-1556
  7. */
  8. class DDC1655Test extends \Doctrine\Tests\OrmFunctionalTestCase
  9. {
  10. public function setUp()
  11. {
  12. parent::setUp();
  13. try {
  14. $this->_schemaTool->createSchema(array(
  15. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Foo'),
  16. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Bar'),
  17. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1655Baz'),
  18. ));
  19. } catch(\Exception $e) {
  20. }
  21. }
  22. public function testPostLoadOneToManyInheritance()
  23. {
  24. $cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Foo');
  25. $this->assertEquals(array("postLoad" => array("postLoad")), $cm->lifecycleCallbacks);
  26. $cm = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1655Bar');
  27. $this->assertEquals(array("postLoad" => array("postLoad", "postSubLoaded")), $cm->lifecycleCallbacks);
  28. $baz = new DDC1655Baz();
  29. $foo = new DDC1655Foo();
  30. $foo->baz = $baz;
  31. $bar = new DDC1655Bar();
  32. $bar->baz = $baz;
  33. $this->_em->persist($foo);
  34. $this->_em->persist($bar);
  35. $this->_em->persist($baz);
  36. $this->_em->flush();
  37. $this->_em->clear();
  38. $baz = $this->_em->find(get_class($baz), $baz->id);
  39. foreach ($baz->foos as $foo) {
  40. $this->assertEquals(1, $foo->loaded, "should have loaded callback counter incremented for " . get_class($foo));
  41. }
  42. }
  43. /**
  44. * Check that post load is not executed several times when the entity
  45. * is rehydrated again although its already known.
  46. */
  47. public function testPostLoadInheritanceChild()
  48. {
  49. $bar = new DDC1655Bar();
  50. $this->_em->persist($bar);
  51. $this->_em->flush();
  52. $this->_em->clear();
  53. $bar = $this->_em->find(get_class($bar), $bar->id);
  54. $this->assertEquals(1, $bar->loaded);
  55. $this->assertEquals(1, $bar->subLoaded);
  56. $bar = $this->_em->find(get_class($bar), $bar->id);
  57. $this->assertEquals(1, $bar->loaded);
  58. $this->assertEquals(1, $bar->subLoaded);
  59. $dql = "SELECT b FROM " . __NAMESPACE__ . "\DDC1655Bar b WHERE b.id = ?1";
  60. $bar = $this->_em->createQuery($dql)->setParameter(1, $bar->id)->getSingleResult();
  61. $this->assertEquals(1, $bar->loaded);
  62. $this->assertEquals(1, $bar->subLoaded);
  63. $this->_em->refresh($bar);
  64. $this->assertEquals(2, $bar->loaded);
  65. $this->assertEquals(2, $bar->subLoaded);
  66. }
  67. }
  68. /**
  69. * @Entity
  70. * @InheritanceType("SINGLE_TABLE")
  71. * @DiscriminatorMap({
  72. * "foo" = "DDC1655Foo",
  73. * "bar" = "DDC1655Bar"
  74. * })
  75. * @HasLifecycleCallbacks
  76. */
  77. class DDC1655Foo
  78. {
  79. /** @Id @GeneratedValue @Column(type="integer") */
  80. public $id;
  81. public $loaded = 0;
  82. /**
  83. * @ManyToOne(targetEntity="DDC1655Baz", inversedBy="foos")
  84. */
  85. public $baz;
  86. /**
  87. * @PostLoad
  88. */
  89. public function postLoad()
  90. {
  91. $this->loaded++;
  92. }
  93. }
  94. /**
  95. * @Entity
  96. * @HasLifecycleCallbacks
  97. */
  98. class DDC1655Bar extends DDC1655Foo
  99. {
  100. public $subLoaded;
  101. /**
  102. * @PostLoad
  103. */
  104. public function postSubLoaded()
  105. {
  106. $this->subLoaded++;
  107. }
  108. }
  109. /**
  110. * @Entity
  111. */
  112. class DDC1655Baz
  113. {
  114. /** @Id @GeneratedValue @Column(type="integer") */
  115. public $id;
  116. /**
  117. * @OneToMany(targetEntity="DDC1655Foo", mappedBy="baz")
  118. */
  119. public $foos = array();
  120. }