Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with single table inheritance and association keys #11611

Draft
wants to merge 1 commit into
base: 3.2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/EntityA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;

#[Entity]
class EntityA extends VersionedEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

public function __construct(int $id)
{
$this->id = $id;
}

#[OneToOne(targetEntity: EntityVersion::class, cascade: ['persist'])]
#[JoinColumn(name: 'id', referencedColumnName: 'entityId', nullable: true, onDelete: 'CASCADE')]
protected EntityVersion|null $version = null;

protected function createVersion(string $version): EntityVersion
{
return new EntityAVersion($this->id, $version);
}
}
12 changes: 12 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/EntityAVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\Mapping\Entity;

#[Entity]
class EntityAVersion extends EntityVersion
{
}
33 changes: 33 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/EntityB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToOne;

#[Entity]
class EntityB extends VersionedEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

public function __construct(int $id)
{
$this->id = $id;
}

#[OneToOne(targetEntity: EntityBVersion::class)]
#[JoinColumn(name: 'id', referencedColumnName: 'entityId')]
protected EntityVersion|null $version = null;

protected function createVersion(string $version): EntityVersion
{
return new EntityBVersion($this->id, $version);
}
}
12 changes: 12 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/EntityBVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\Mapping\Entity;

#[Entity]
class EntityBVersion extends EntityVersion
{
}
48 changes: 48 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/EntityVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\Table;

#[Table(name: 'entity_version')]
#[Entity]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorMap([EntityA::class => EntityAVersion::class, EntityB::class => EntityBVersion::class])]
#[DiscriminatorColumn(name: 'entityType', type: 'string')]
abstract class EntityVersion
{
/** @var class-string */
#[Id]
public $entityType;

/** @var int */
#[Id]
#[Column(type: 'integer')]
public $entityId;

/** @var string */
#[Column(type: 'string')]
public $version;

public function __construct(
int $entityId,
string $version,
) {
$this->entityType = static::class;
$this->entityId = $entityId;
$this->version = $version;
}

public function setVersion(string $version): void
{
$this->version = $version;
}
}
51 changes: 51 additions & 0 deletions tests/Tests/Models/IssueKanbanBOX/VersionedEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\IssueKanbanBOX;

use Doctrine\ORM\EntityNotFoundException;
use Doctrine\Persistence\Proxy;
use ReflectionProperty;

abstract class VersionedEntity
{
protected EntityVersion|null $version = null;

abstract protected function createVersion(string $version): EntityVersion;

public function setVersion(string $version): void
{
//$this->ensureJoinedEntityPropertyInitialized($this, 'version');

if ($this->version === null) {
$this->version = $this->createVersion($version);

return;
}

$this->version->version = $version;
}

public function getVersion(): string|null
{
//$this->ensureJoinedEntityPropertyInitialized($this, 'version');

return $this->version?->version;
}

private function ensureJoinedEntityPropertyInitialized(object $entity, string $propertyName): void
{
$reflection = new ReflectionProperty($entity, $propertyName);
$value = $reflection->getValue($entity);
if (! ($value instanceof Proxy)) {
return;
}

try {
$value->__load();
} catch (EntityNotFoundException) {
$reflection->setValue($entity, null);
}
}
}
28 changes: 28 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/IssueKanbanBOXTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\IssueKanbanBOX\EntityA;
use Doctrine\Tests\OrmFunctionalTestCase;

class IssueKanbanBOXTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('issueKanbanBOX');

parent::setUp();
}

public function testEntityVersion(): void
{
$entityA = new EntityA(1);
$this->_em->persist($entityA);
$this->_em->flush();
$this->_em->clear();
$dbRetrievedEntityA = $this->_em->getRepository(EntityA::class)->find(1);
self::assertNull($dbRetrievedEntityA->getVersion());
}
}
7 changes: 7 additions & 0 deletions tests/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
Models\Issue9300\Issue9300Child::class,
Models\Issue9300\Issue9300Parent::class,
],
'issueKanbanBOX' => [
Models\IssueKanbanBOX\EntityVersion::class,
Models\IssueKanbanBOX\EntityAVersion::class,
Models\IssueKanbanBOX\EntityBVersion::class,
Models\IssueKanbanBOX\EntityA::class,
Models\IssueKanbanBOX\EntityB::class,
],
];

/** @param class-string ...$models */
Expand Down
Loading