OrderedJoinedTableInheritanceCollectionTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\Query;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. /**
  6. * Functional tests for the Single Table Inheritance mapping strategy.
  7. *
  8. * @author Benjamin Eberlei <kontakt@beberlei.de>
  9. */
  10. class OrderedJoinedTableInheritanceCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected function setUp() {
  13. parent::setUp();
  14. try {
  15. $this->_schemaTool->createSchema(array(
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\OJTIC_Pet'),
  17. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\OJTIC_Cat'),
  18. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\OJTIC_Dog'),
  19. ));
  20. } catch (\Exception $e) {
  21. // Swallow all exceptions. We do not test the schema tool here.
  22. }
  23. $dog = new OJTIC_Dog();
  24. $dog->name = "Poofy";
  25. $dog1 = new OJTIC_Dog();
  26. $dog1->name = "Zampa";
  27. $dog2 = new OJTIC_Dog();
  28. $dog2->name = "Aari";
  29. $dog1->mother = $dog;
  30. $dog2->mother = $dog;
  31. $dog->children[] = $dog1;
  32. $dog->children[] = $dog2;
  33. $this->_em->persist($dog);
  34. $this->_em->persist($dog1);
  35. $this->_em->persist($dog2);
  36. $this->_em->flush();
  37. $this->_em->clear();
  38. }
  39. public function testOrderdOneToManyCollection()
  40. {
  41. $poofy = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\OJTIC_Pet p WHERE p.name = 'Poofy'")->getSingleResult();
  42. $this->assertEquals('Aari', $poofy->children[0]->getName());
  43. $this->assertEquals('Zampa', $poofy->children[1]->getName());
  44. $this->_em->clear();
  45. $result = $this->_em->createQuery(
  46. "SELECT p, c FROM Doctrine\Tests\ORM\Functional\OJTIC_Pet p JOIN p.children c WHERE p.name = 'Poofy'")
  47. ->getResult();
  48. $this->assertEquals(1, count($result));
  49. $poofy = $result[0];
  50. $this->assertEquals('Aari', $poofy->children[0]->getName());
  51. $this->assertEquals('Zampa', $poofy->children[1]->getName());
  52. }
  53. }
  54. /**
  55. * @Entity
  56. * @InheritanceType("JOINED")
  57. * @DiscriminatorColumn(name="discr", type="string")
  58. * @DiscriminatorMap({
  59. * "cat" = "OJTIC_Cat",
  60. * "dog" = "OJTIC_Dog"})
  61. */
  62. abstract class OJTIC_Pet
  63. {
  64. /**
  65. * @Id
  66. * @column(type="integer")
  67. * @generatedValue(strategy="AUTO")
  68. */
  69. public $id;
  70. /**
  71. *
  72. * @Column
  73. */
  74. public $name;
  75. /**
  76. * @ManyToOne(targetEntity="OJTIC_PET")
  77. */
  78. public $mother;
  79. /**
  80. * @OneToMany(targetEntity="OJTIC_Pet", mappedBy="mother")
  81. * @OrderBy({"name" = "ASC"})
  82. */
  83. public $children;
  84. /**
  85. * @ManyToMany(targetEntity="OJTIC_Pet")
  86. * @JoinTable(name="OTJIC_Pet_Friends",
  87. * joinColumns={@JoinColumn(name="pet_id", referencedColumnName="id")},
  88. * inverseJoinColumns={@JoinColumn(name="friend_id", referencedColumnName="id")})
  89. * @OrderBy({"name" = "ASC"})
  90. */
  91. public $friends;
  92. public function getName()
  93. {
  94. return $this->name;
  95. }
  96. }
  97. /**
  98. * @Entity
  99. */
  100. class OJTIC_Cat extends OJTIC_Pet
  101. {
  102. }
  103. /**
  104. * @Entity
  105. */
  106. class OJTIC_Dog extends OJTIC_Pet
  107. {
  108. }