1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
/**
* Tests a self referential one-to-one association mapping (without inheritance).
* Relation is defined as the mentor that a customer choose. The mentor could
* help only one other customer, while a customer can choose only one mentor
* for receiving support.
* Inverse side is not present.
*/
class OneToOneSelfReferentialAssociationTest extends OrmFunctionalTestCase
{
private ECommerceCustomer $customer;
private ECommerceCustomer $mentor;
protected function setUp(): void
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->customer = new ECommerceCustomer();
$this->customer->setName('Anakin Skywalker');
$this->mentor = new ECommerceCustomer();
$this->mentor->setName('Obi-wan Kenobi');
}
public function testSavesAOneToOneAssociationWithCascadeSaveSet(): void
{
$this->customer->setMentor($this->mentor);
$this->_em->persist($this->customer);
$this->_em->flush();
$this->assertForeignKeyIs($this->mentor->getId());
}
public function testRemovesOneToOneAssociation(): void
{
$this->customer->setMentor($this->mentor);
$this->_em->persist($this->customer);
$this->customer->removeMentor();
$this->_em->flush();
$this->assertForeignKeyIs(null);
}
public function testFind(): void
{
$id = $this->createFixture();
$customer = $this->_em->find(ECommerceCustomer::class, $id);
self::assertFalse($this->isUninitializedObject($customer->getMentor()));
}
public function testEagerLoadsAssociation(): void
{
$customerId = $this->createFixture();
$query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m where c.id = :id');
$query->setParameter('id', $customerId);
$result = $query->getResult();
$customer = $result[0];
$this->assertLoadingOfAssociation($customer);
}
#[Group('mine')]
public function testLazyLoadsAssociation(): void
{
$this->createFixture();
$metadata = $this->_em->getClassMetadata(ECommerceCustomer::class);
$metadata->associationMappings['mentor']->fetch = ClassMetadata::FETCH_LAZY;
$query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
$result = $query->getResult();
$customer = $result[0];
$this->assertLoadingOfAssociation($customer);
}
public function testMultiSelfReference(): void
{
$this->createSchemaForModels(MultiSelfReference::class);
$entity1 = new MultiSelfReference();
$this->_em->persist($entity1);
$entity1->setOther1($entity2 = new MultiSelfReference());
$entity1->setOther2($entity3 = new MultiSelfReference());
$this->_em->flush();
$this->_em->clear();
$entity2 = $this->_em->find($entity1::class, $entity1->getId());
self::assertInstanceOf(MultiSelfReference::class, $entity2->getOther1());
self::assertInstanceOf(MultiSelfReference::class, $entity2->getOther2());
self::assertNull($entity2->getOther1()->getOther1());
self::assertNull($entity2->getOther1()->getOther2());
self::assertNull($entity2->getOther2()->getOther1());
self::assertNull($entity2->getOther2()->getOther2());
}
public function assertLoadingOfAssociation($customer): void
{
self::assertInstanceOf(ECommerceCustomer::class, $customer->getMentor());
self::assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
}
public function assertForeignKeyIs($value): void
{
$foreignKey = $this->_em->getConnection()->executeQuery('SELECT mentor_id FROM ecommerce_customers WHERE id=?', [$this->customer->getId()])->fetchOne();
self::assertEquals($value, $foreignKey);
}
private function createFixture(): int
{
$customer = new ECommerceCustomer();
$customer->setName('Luke Skywalker');
$mentor = new ECommerceCustomer();
$mentor->setName('Obi-wan Kenobi');
$customer->setMentor($mentor);
$this->_em->persist($customer);
$this->_em->flush();
$this->_em->clear();
return $customer->getId();
}
}
#[Entity]
class MultiSelfReference
{
#[Id]
#[GeneratedValue(strategy: 'AUTO')]
#[Column(type: 'integer')]
private int $id;
#[OneToOne(targetEntity: 'MultiSelfReference', cascade: ['persist'])]
#[JoinColumn(name: 'other1', referencedColumnName: 'id')]
private MultiSelfReference|null $other1 = null;
#[OneToOne(targetEntity: 'MultiSelfReference', cascade: ['persist'])]
#[JoinColumn(name: 'other2', referencedColumnName: 'id')]
private MultiSelfReference|null $other2 = null;
public function getId(): int
{
return $this->id;
}
public function setOther1(MultiSelfReference $other1): void
{
$this->other1 = $other1;
}
public function getOther1(): MultiSelfReference|null
{
return $this->other1;
}
public function setOther2(MultiSelfReference $other2): void
{
$this->other2 = $other2;
}
public function getOther2(): MultiSelfReference|null
{
return $this->other2;
}
}
|