OneToOneSelfReferentialAssociationTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
  4. use Doctrine\ORM\Mapping\AssociationMapping;
  5. use Doctrine\ORM\Mapping\ClassMetadata;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * Tests a self referential one-to-one association mapping (without inheritance).
  9. * Relation is defined as the mentor that a customer choose. The mentor could
  10. * help only one other customer, while a customer can choose only one mentor
  11. * for receiving support.
  12. * Inverse side is not present.
  13. */
  14. class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  15. {
  16. private $customer;
  17. private $mentor;
  18. protected function setUp()
  19. {
  20. $this->useModelSet('ecommerce');
  21. parent::setUp();
  22. $this->customer = new ECommerceCustomer();
  23. $this->customer->setName('Anakin Skywalker');
  24. $this->mentor = new ECommerceCustomer();
  25. $this->mentor->setName('Obi-wan Kenobi');
  26. }
  27. public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
  28. $this->customer->setMentor($this->mentor);
  29. $this->_em->persist($this->customer);
  30. $this->_em->flush();
  31. $this->assertForeignKeyIs($this->mentor->getId());
  32. }
  33. public function testRemovesOneToOneAssociation()
  34. {
  35. $this->customer->setMentor($this->mentor);
  36. $this->_em->persist($this->customer);
  37. $this->customer->removeMentor();
  38. $this->_em->flush();
  39. $this->assertForeignKeyIs(null);
  40. }
  41. public function testFind()
  42. {
  43. $id = $this->_createFixture();
  44. $customer = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $id);
  45. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getMentor());
  46. }
  47. public function testEagerLoadsAssociation()
  48. {
  49. $this->_createFixture();
  50. $query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc');
  51. $result = $query->getResult();
  52. $customer = $result[0];
  53. $this->assertLoadingOfAssociation($customer);
  54. }
  55. /**
  56. * @group mine
  57. * @return unknown_type
  58. */
  59. public function testLazyLoadsAssociation()
  60. {
  61. $this->_createFixture();
  62. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
  63. $metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_LAZY;
  64. $query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
  65. $result = $query->getResult();
  66. $customer = $result[0];
  67. $this->assertLoadingOfAssociation($customer);
  68. }
  69. public function testMultiSelfReference()
  70. {
  71. try {
  72. $this->_schemaTool->createSchema(array(
  73. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\MultiSelfReference')
  74. ));
  75. } catch (\Exception $e) {
  76. // Swallow all exceptions. We do not test the schema tool here.
  77. }
  78. $entity1 = new MultiSelfReference();
  79. $this->_em->persist($entity1);
  80. $entity1->setOther1($entity2 = new MultiSelfReference);
  81. $entity1->setOther2($entity3 = new MultiSelfReference);
  82. $this->_em->flush();
  83. $this->_em->clear();
  84. $entity2 = $this->_em->find(get_class($entity1), $entity1->getId());
  85. $this->assertTrue($entity2->getOther1() instanceof MultiSelfReference);
  86. $this->assertTrue($entity2->getOther2() instanceof MultiSelfReference);
  87. $this->assertNull($entity2->getOther1()->getOther1());
  88. $this->assertNull($entity2->getOther1()->getOther2());
  89. $this->assertNull($entity2->getOther2()->getOther1());
  90. $this->assertNull($entity2->getOther2()->getOther2());
  91. }
  92. public function assertLoadingOfAssociation($customer)
  93. {
  94. $this->assertTrue($customer->getMentor() instanceof ECommerceCustomer);
  95. $this->assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
  96. }
  97. public function assertForeignKeyIs($value) {
  98. $foreignKey = $this->_em->getConnection()->executeQuery('SELECT mentor_id FROM ecommerce_customers WHERE id=?', array($this->customer->getId()))->fetchColumn();
  99. $this->assertEquals($value, $foreignKey);
  100. }
  101. private function _createFixture()
  102. {
  103. $customer = new ECommerceCustomer;
  104. $customer->setName('Luke Skywalker');
  105. $mentor = new ECommerceCustomer;
  106. $mentor->setName('Obi-wan Kenobi');
  107. $customer->setMentor($mentor);
  108. $this->_em->persist($customer);
  109. $this->_em->flush();
  110. $this->_em->clear();
  111. return $customer->getId();
  112. }
  113. }
  114. /**
  115. * @Entity
  116. */
  117. class MultiSelfReference {
  118. /** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") */
  119. private $id;
  120. /**
  121. * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
  122. * @JoinColumn(name="other1", referencedColumnName="id")
  123. */
  124. private $other1;
  125. /**
  126. * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
  127. * @JoinColumn(name="other2", referencedColumnName="id")
  128. */
  129. private $other2;
  130. public function getId() {return $this->id;}
  131. public function setOther1($other1) {$this->other1 = $other1;}
  132. public function getOther1() {return $this->other1;}
  133. public function setOther2($other2) {$this->other2 = $other2;}
  134. public function getOther2() {return $this->other2;}
  135. }