DDC531Test.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. require_once __DIR__ . '/../../../TestInit.php';
  4. class DDC531Test extends \Doctrine\Tests\OrmFunctionalTestCase
  5. {
  6. protected function setUp()
  7. {
  8. parent::setUp();
  9. $this->_schemaTool->createSchema(array(
  10. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC531Item'),
  11. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC531SubItem'),
  12. ));
  13. }
  14. public function testIssue()
  15. {
  16. $item1 = new DDC531Item;
  17. $item2 = new DDC531Item;
  18. $item2->parent = $item1;
  19. $item1->getChildren()->add($item2);
  20. $this->_em->persist($item1);
  21. $this->_em->persist($item2);
  22. $this->_em->flush();
  23. $this->_em->clear();
  24. $item3 = $this->_em->find(__NAMESPACE__ . '\DDC531Item', $item2->id); // Load child item first (id 2)
  25. // parent will already be loaded, cannot be lazy because it has mapped subclasses and we would not
  26. // know which proxy type to put in.
  27. $this->assertInstanceOf(__NAMESPACE__ . '\DDC531Item', $item3->parent);
  28. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $item3->parent);
  29. $item4 = $this->_em->find(__NAMESPACE__ . '\DDC531Item', $item1->id); // Load parent item (id 1)
  30. $this->assertNull($item4->parent);
  31. $this->assertNotNull($item4->getChildren());
  32. $this->assertTrue($item4->getChildren()->contains($item3)); // lazy-loads children
  33. }
  34. }
  35. /**
  36. * @Entity
  37. * @InheritanceType("SINGLE_TABLE")
  38. * @DiscriminatorColumn(name="type", type="integer")
  39. * @DiscriminatorMap({"0" = "DDC531Item", "1" = "DDC531SubItem"})
  40. */
  41. class DDC531Item
  42. {
  43. /**
  44. * @Id
  45. * @Column(type="integer")
  46. * @GeneratedValue(strategy="AUTO")
  47. */
  48. public $id;
  49. /**
  50. * @OneToMany(targetEntity="DDC531Item", mappedBy="parent")
  51. */
  52. protected $children;
  53. /**
  54. * @ManyToOne(targetEntity="DDC531Item", inversedBy="children")
  55. * @JoinColumn(name="parentId", referencedColumnName="id")
  56. */
  57. public $parent;
  58. public function __construct()
  59. {
  60. $this->children = new \Doctrine\Common\Collections\ArrayCollection;
  61. }
  62. public function getParent()
  63. {
  64. return $this->parent;
  65. }
  66. public function getChildren()
  67. {
  68. return $this->children;
  69. }
  70. }
  71. /**
  72. * @Entity
  73. */
  74. class DDC531SubItem extends DDC531Item
  75. {
  76. }