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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
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\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
use function assert;
/**
* Functional tests for the Single Table Inheritance mapping strategy.
*/
class DDC69Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
// some of these models are created in other tests, but not all
$this->createSchemaForModels(RelationType::class);
$this->createSchemaForModels(Relation::class);
$this->createSchemaForModels(Lemma::class);
}
public function testIssue(): void
{
//setup
$lemma1 = new Lemma();
$lemma1->setLemma('foo');
$lemma2 = new Lemma();
$lemma2->setLemma('bar');
$lemma3 = new Lemma();
$lemma3->setLemma('batz');
$lemma4 = new Lemma();
$lemma4->setLemma('bla');
$type1 = new RelationType();
$type1->setType('nonsense');
$type1->setAbbreviation('non');
$type2 = new RelationType();
$type2->setType('quatsch');
$type2->setAbbreviation('qu');
$relation1 = new Relation();
$relation1->setParent($lemma1);
$relation1->setChild($lemma2);
$relation1->setType($type1);
$relation2 = new Relation();
$relation2->setParent($lemma1);
$relation2->setChild($lemma3);
$relation2->setType($type1);
$relation3 = new Relation();
$relation3->setParent($lemma1);
$relation3->setChild($lemma4);
$relation3->setType($type2);
$lemma1->addRelation($relation1);
$lemma1->addRelation($relation2);
$lemma1->addRelation($relation3);
$this->_em->persist($type1);
$this->_em->persist($type2);
$this->_em->persist($lemma1);
$this->_em->persist($lemma2);
$this->_em->persist($lemma3);
$this->_em->persist($lemma4);
$this->_em->flush();
$this->_em->clear();
//end setup
// test One To Many
$query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Ticket\Lemma l Where l.lemma = 'foo'");
$res = $query->getResult();
$lemma = $res[0];
self::assertEquals('foo', $lemma->getLemma());
self::assertInstanceOf(Lemma::class, $lemma);
$relations = $lemma->getRelations();
foreach ($relations as $relation) {
self::assertInstanceOf(Relation::class, $relation);
self::assertTrue($relation->getType()->getType() !== '');
}
$this->_em->clear();
}
}
#[Table(name: 'lemma')]
#[Entity]
class Lemma
{
public const CLASS_NAME = self::class;
#[Id]
#[Column(type: 'integer', name: 'lemma_id')]
#[GeneratedValue(strategy: 'AUTO')]
private int $id;
#[Column(type: 'string', name: 'lemma_name', unique: true, length: 255)]
private string|null $lemma = null;
/** @var Collection<int, Relation> */
#[OneToMany(targetEntity: 'Relation', mappedBy: 'parent', cascade: ['persist'])]
private Collection $relations;
public function __construct()
{
$this->relations = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function setLemma(string $lemma): void
{
$this->lemma = $lemma;
}
public function getLemma(): string
{
return $this->lemma;
}
public function addRelation(Relation $relation): void
{
$this->relations[] = $relation;
$relation->setParent($this);
}
public function removeRelation(Relation $relation): void
{
$removed = $this->relations->removeElement($relation);
assert($removed instanceof Relation);
if ($removed !== null) {
$removed->removeParent();
}
}
/** @phpstan-return Collection<int, Relation> */
public function getRelations(): Collection
{
return $this->relations;
}
}
#[Table(name: 'relation')]
#[Entity]
class Relation
{
public const CLASS_NAME = self::class;
#[Id]
#[Column(type: 'integer', name: 'relation_id')]
#[GeneratedValue(strategy: 'AUTO')]
private int $id;
#[ManyToOne(targetEntity: 'Lemma', inversedBy: 'relations')]
#[JoinColumn(name: 'relation_parent_id', referencedColumnName: 'lemma_id')]
private Lemma|null $parent = null;
#[OneToOne(targetEntity: 'Lemma')]
#[JoinColumn(name: 'relation_child_id', referencedColumnName: 'lemma_id')]
private Lemma|null $child = null;
#[ManyToOne(targetEntity: 'RelationType', inversedBy: 'relations')]
#[JoinColumn(name: 'relation_type_id', referencedColumnName: 'relation_type_id')]
private RelationType|null $type = null;
public function setParent(Lemma $parent): void
{
$this->parent = $parent;
}
public function getParent(): Phrase
{
return $this->parent;
}
public function removeParent(): void
{
if ($this->lemma !== null) {
$lemma = $this->parent;
$this->parent = null;
$lemma->removeRelation($this);
}
}
public function setChild(Lemma $child): void
{
$this->child = $child;
}
public function getChild(): Lemma
{
return $this->child;
}
public function setType(RelationType $type): void
{
$this->type = $type;
}
public function getType(): RelationType
{
return $this->type;
}
public function removeType(): void
{
if ($this->type !== null) {
$type = $this->type;
$this->type = null;
$type->removeRelation($this);
}
}
}
#[Table(name: 'relation_type')]
#[Entity]
class RelationType
{
public const CLASS_NAME = self::class;
#[Id]
#[Column(type: 'integer', name: 'relation_type_id')]
#[GeneratedValue(strategy: 'AUTO')]
private int $id;
#[Column(type: 'string', name: 'relation_type_name', unique: true, length: 255)]
private string|null $type = null;
#[Column(type: 'string', name: 'relation_type_abbreviation', unique: true, length: 255)]
private string|null $abbreviation = null;
/** @var Collection<int, Relation> */
#[OneToMany(targetEntity: 'Relation', mappedBy: 'type', cascade: ['persist'])]
private $relations;
public function __construct()
{
$relations = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function setType(string $type): void
{
$this->type = $type;
}
public function getType(): string
{
return $this->type;
}
public function setAbbreviation(string $abbreviation): void
{
$this->abbreviation = $abbreviation;
}
public function getAbbreviation(): string
{
return $this->abbreviation;
}
public function addRelation(Relation $relation): void
{
$this->relations[] = $relation;
$relation->setType($this);
}
public function removeRelation(Relation $relation): void
{
$removed = $this->relations->removeElement($relation);
assert($removed instanceof Relation);
if ($removed !== null) {
$removed->removeLemma();
}
}
/** @phpstan-return Collection<int, Relation> */
public function getRelations(): Collection
{
return $this->relations;
}
}
|