3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
6 use Doctrine\ORM\Mapping\AssociationMapping;
7 use Doctrine\ORM\Mapping\ClassMetadata;
9 require_once __DIR__ . '/../../TestInit.php';
12 * Tests a self referential one-to-one association mapping (without inheritance).
13 * Relation is defined as the mentor that a customer choose. The mentor could
14 * help only one other customer, while a customer can choose only one mentor
15 * for receiving support.
16 * Inverse side is not present.
18 class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
23 protected function setUp()
25 $this->useModelSet('ecommerce');
27 $this->customer = new ECommerceCustomer();
28 $this->customer->setName('Anakin Skywalker');
29 $this->mentor = new ECommerceCustomer();
30 $this->mentor->setName('Obi-wan Kenobi');
33 public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
34 $this->customer->setMentor($this->mentor);
35 $this->_em->persist($this->customer);
38 $this->assertForeignKeyIs($this->mentor->getId());
41 public function testRemovesOneToOneAssociation()
43 $this->customer->setMentor($this->mentor);
44 $this->_em->persist($this->customer);
45 $this->customer->removeMentor();
49 $this->assertForeignKeyIs(null);
52 public function testFind()
54 $id = $this->_createFixture();
56 $customer = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $id);
57 $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getMentor());
60 public function testEagerLoadsAssociation()
62 $this->_createFixture();
64 $query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc');
65 $result = $query->getResult();
66 $customer = $result[0];
67 $this->assertLoadingOfAssociation($customer);
72 * @return unknown_type
74 public function testLazyLoadsAssociation()
76 $this->_createFixture();
78 $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
79 $metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_LAZY;
81 $query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
82 $result = $query->getResult();
83 $customer = $result[0];
84 $this->assertLoadingOfAssociation($customer);
87 public function testMultiSelfReference()
90 $this->_schemaTool->createSchema(array(
91 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\MultiSelfReference')
93 } catch (\Exception $e) {
94 // Swallow all exceptions. We do not test the schema tool here.
97 $entity1 = new MultiSelfReference();
98 $this->_em->persist($entity1);
99 $entity1->setOther1($entity2 = new MultiSelfReference);
100 $entity1->setOther2($entity3 = new MultiSelfReference);
105 $entity2 = $this->_em->find(get_class($entity1), $entity1->getId());
107 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\MultiSelfReference', $entity2->getOther1());
108 $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\MultiSelfReference', $entity2->getOther2());
109 $this->assertNull($entity2->getOther1()->getOther1());
110 $this->assertNull($entity2->getOther1()->getOther2());
111 $this->assertNull($entity2->getOther2()->getOther1());
112 $this->assertNull($entity2->getOther2()->getOther2());
115 public function assertLoadingOfAssociation($customer)
117 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $customer->getMentor());
118 $this->assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
121 public function assertForeignKeyIs($value) {
122 $foreignKey = $this->_em->getConnection()->executeQuery('SELECT mentor_id FROM ecommerce_customers WHERE id=?', array($this->customer->getId()))->fetchColumn();
123 $this->assertEquals($value, $foreignKey);
126 private function _createFixture()
128 $customer = new ECommerceCustomer;
129 $customer->setName('Luke Skywalker');
130 $mentor = new ECommerceCustomer;
131 $mentor->setName('Obi-wan Kenobi');
132 $customer->setMentor($mentor);
134 $this->_em->persist($customer);
139 return $customer->getId();
146 class MultiSelfReference {
147 /** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") */
150 * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
151 * @JoinColumn(name="other1", referencedColumnName="id")
155 * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
156 * @JoinColumn(name="other2", referencedColumnName="id")
160 public function getId() {return $this->id;}
161 public function setOther1($other1) {$this->other1 = $other1;}
162 public function getOther1() {return $this->other1;}
163 public function setOther2($other2) {$this->other2 = $other2;}
164 public function getOther2() {return $this->other2;}