ClassTableInheritanceTest2.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. /**
  5. * Functional tests for the Class Table Inheritance mapping strategy.
  6. *
  7. * @author robo
  8. */
  9. class ClassTableInheritanceTest2 extends \Doctrine\Tests\OrmFunctionalTestCase
  10. {
  11. protected function setUp() {
  12. parent::setUp();
  13. try {
  14. $this->_schemaTool->createSchema(array(
  15. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIParent'),
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIChild'),
  17. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIRelated'),
  18. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIRelated2')
  19. ));
  20. } catch (\Exception $ignored) {
  21. // Swallow all exceptions. We do not test the schema tool here.
  22. }
  23. }
  24. public function testOneToOneAssocToBaseTypeBidirectional()
  25. {
  26. $child = new CTIChild;
  27. $child->setData('hello');
  28. $related = new CTIRelated;
  29. $related->setCTIParent($child);
  30. $this->_em->persist($related);
  31. $this->_em->persist($child);
  32. $this->_em->flush();
  33. $this->_em->clear();
  34. $relatedId = $related->getId();
  35. $related2 = $this->_em->find('Doctrine\Tests\ORM\Functional\CTIRelated', $relatedId);
  36. $this->assertTrue($related2 instanceof CTIRelated);
  37. $this->assertTrue($related2->getCTIParent() instanceof CTIChild);
  38. $this->assertFalse($related2->getCTIParent() instanceof \Doctrine\ORM\Proxy\Proxy);
  39. $this->assertEquals('hello', $related2->getCTIParent()->getData());
  40. $this->assertSame($related2, $related2->getCTIParent()->getRelated());
  41. }
  42. public function testManyToManyToCTIHierarchy()
  43. {
  44. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  45. $mmrel = new CTIRelated2;
  46. $child = new CTIChild;
  47. $child->setData('child');
  48. $mmrel->addCTIChild($child);
  49. $this->_em->persist($mmrel);
  50. $this->_em->persist($child);
  51. $this->_em->flush();
  52. $this->_em->clear();
  53. $mmrel2 = $this->_em->find(get_class($mmrel), $mmrel->getId());
  54. $this->assertFalse($mmrel2->getCTIChildren()->isInitialized());
  55. $this->assertEquals(1, count($mmrel2->getCTIChildren()));
  56. $this->assertTrue($mmrel2->getCTIChildren()->isInitialized());
  57. $this->assertTrue($mmrel2->getCTIChildren()->get(0) instanceof CTIChild);
  58. }
  59. }
  60. /**
  61. * @Entity @Table(name="cti_parents")
  62. * @InheritanceType("JOINED")
  63. * @DiscriminatorColumn(name="type", type="string")
  64. * @DiscriminatorMap({"parent" = "CTIParent", "child" = "CTIChild"})
  65. */
  66. class CTIParent {
  67. /**
  68. * @Id @Column(type="integer")
  69. * @GeneratedValue(strategy="AUTO")
  70. */
  71. private $id;
  72. /** @OneToOne(targetEntity="CTIRelated", mappedBy="ctiParent") */
  73. private $related;
  74. public function getId() {
  75. return $this->id;
  76. }
  77. public function getRelated() {
  78. return $this->related;
  79. }
  80. public function setRelated($related) {
  81. $this->related = $related;
  82. $related->setCTIParent($this);
  83. }
  84. }
  85. /**
  86. * @Entity @Table(name="cti_children")
  87. */
  88. class CTIChild extends CTIParent {
  89. /**
  90. * @Column(type="string")
  91. */
  92. private $data;
  93. public function getData() {
  94. return $this->data;
  95. }
  96. public function setData($data) {
  97. $this->data = $data;
  98. }
  99. }
  100. /** @Entity */
  101. class CTIRelated {
  102. /**
  103. * @Id @Column(type="integer")
  104. * @GeneratedValue(strategy="AUTO")
  105. */
  106. private $id;
  107. /**
  108. * @OneToOne(targetEntity="CTIParent")
  109. * @JoinColumn(name="ctiparent_id", referencedColumnName="id")
  110. */
  111. private $ctiParent;
  112. public function getId() {
  113. return $this->id;
  114. }
  115. public function getCTIParent() {
  116. return $this->ctiParent;
  117. }
  118. public function setCTIParent($ctiParent) {
  119. $this->ctiParent = $ctiParent;
  120. }
  121. }
  122. /** @Entity */
  123. class CTIRelated2
  124. {
  125. /** @Id @Column(type="integer") @GeneratedValue */
  126. private $id;
  127. /** @ManyToMany(targetEntity="CTIChild") */
  128. private $ctiChildren;
  129. public function __construct() {
  130. $this->ctiChildren = new \Doctrine\Common\Collections\ArrayCollection;
  131. }
  132. public function getId() {
  133. return $this->id;
  134. }
  135. public function addCTIChild(CTIChild $child) {
  136. $this->ctiChildren->add($child);
  137. }
  138. public function getCTIChildren() {
  139. return $this->ctiChildren;
  140. }
  141. }