DDC992Test.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. /**
  6. * @group DDC-992
  7. */
  8. class DDC992Test 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__ . '\DDC992Role'),
  16. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC992Parent'),
  17. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC992Child'),
  18. ));
  19. } catch(\Exception $e) {
  20. }
  21. }
  22. public function testIssue()
  23. {
  24. $role = new DDC992Role();
  25. $role->name = "Parent";
  26. $child = new DDC992Role();
  27. $child->name = "child";
  28. $role->extendedBy[] = $child;
  29. $child->extends[] = $role;
  30. $this->_em->persist($role);
  31. $this->_em->persist($child);
  32. $this->_em->flush();
  33. $this->_em->clear();
  34. $child = $this->_em->getRepository(get_class($role))->find($child->roleID);
  35. $parents = count($child->extends);
  36. $this->assertEquals(1, $parents);
  37. foreach ($child->extends AS $parent) {
  38. $this->assertEquals($role->getRoleID(), $parent->getRoleID());
  39. }
  40. }
  41. public function testOneToManyChild()
  42. {
  43. $parent = new DDC992Parent();
  44. $child = new DDC992Child();
  45. $child->parent = $parent;
  46. $parent->childs[] = $child;
  47. $this->_em->persist($parent);
  48. $this->_em->persist($child);
  49. $this->_em->flush();
  50. $this->_em->clear();
  51. $parentRepository = $this->_em->getRepository(get_class($parent));
  52. $childRepository = $this->_em->getRepository(get_class($child));
  53. $parent = $parentRepository->find($parent->id);
  54. $this->assertEquals(1, count($parent->childs));
  55. $this->assertEquals(0, count($parent->childs[0]->childs()));
  56. $child = $parentRepository->findOneBy(array("id" => $child->id));
  57. $this->assertSame($parent->childs[0], $child);
  58. $this->_em->clear();
  59. $child = $parentRepository->find($child->id);
  60. $this->assertEquals(0, count($child->childs));
  61. $this->_em->clear();
  62. $child = $childRepository->find($child->id);
  63. $this->assertEquals(0, count($child->childs));
  64. }
  65. }
  66. /**
  67. * @Entity
  68. * @InheritanceType("JOINED")
  69. * @DiscriminatorMap({"child" = "DDC992Child", "parent" = "DDC992Parent"})
  70. */
  71. class DDC992Parent
  72. {
  73. /** @Id @GeneratedValue @Column(type="integer") */
  74. public $id;
  75. /** @ManyToOne(targetEntity="DDC992Parent", inversedBy="childs") */
  76. public $parent;
  77. /** @OneToMany(targetEntity="DDC992Child", mappedBy="parent") */
  78. public $childs;
  79. }
  80. /**
  81. * @Entity
  82. */
  83. class DDC992Child extends DDC992Parent
  84. {
  85. public function childs()
  86. {
  87. return $this->childs;
  88. }
  89. }
  90. /**
  91. * @Entity
  92. */
  93. class DDC992Role
  94. {
  95. public function getRoleID()
  96. {
  97. return $this->roleID;
  98. }
  99. /**
  100. * @Id @Column(name="roleID", type="integer")
  101. * @GeneratedValue(strategy="AUTO")
  102. */
  103. public $roleID;
  104. /**
  105. * @Column (name="name", type="string", length="45")
  106. */
  107. public $name;
  108. /**
  109. * @ManyToMany (targetEntity="DDC992Role", mappedBy="extends")
  110. */
  111. public $extendedBy;
  112. /**
  113. * @ManyToMany (targetEntity="DDC992Role", inversedBy="extendedBy")
  114. * @JoinTable (name="RoleRelations",
  115. * joinColumns={@JoinColumn(name="roleID", referencedColumnName="roleID")},
  116. * inverseJoinColumns={@JoinColumn(name="extendsRoleID", referencedColumnName="roleID")}
  117. * )
  118. */
  119. public $extends;
  120. public function __construct() {
  121. $this->extends = new ArrayCollection;
  122. $this->extendedBy = new ArrayCollection;
  123. }
  124. }