Skip to content
Open
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
78 changes: 78 additions & 0 deletions tests/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\MappingException;
Expand Down Expand Up @@ -581,6 +585,41 @@ private static function columnIsIndexed(DbalTable $table, string $column): bool

return false;
}

public function testSingleTableInheritanceWithSameForeignKeyInChildrenDoesNotTriggerDeprecation(): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

$schema = $schemaTool->getSchemaFromMetadata([
$em->getClassMetadata(STIBox::class),
$em->getClassMetadata(STILocation::class),
$em->getClassMetadata(STIBoxContainer::class),
$em->getClassMetadata(STIPackedBoxContainer::class),
]);

self::assertTrue($schema->hasTable('sti_location'));

$table = $schema->getTable('sti_location');

self::assertCount(1, $table->getForeignKeys());

$foreignKeys = $table->getForeignKeys();
$foreignKey = current($foreignKeys);

if (! class_exists(ForeignKeyConstraintEditor::class)) {
self::assertSame('sti_box', $foreignKey->getForeignTableName());
self::assertSame(['box_id'], $foreignKey->getLocalColumns());

return;
}
Comment thread
janedbal marked this conversation as resolved.

self::assertSame('sti_box', $foreignKey->getReferencedTableName()->toString());
self::assertSame(['box_id'], array_map(
static fn (UnqualifiedName $name) => $name->toString(),
$foreignKey->getReferencingColumnNames(),
));
}
}

#[Table(options: ['foo' => 'bar', 'baz' => ['key' => 'val']])]
Expand Down Expand Up @@ -816,3 +855,42 @@ class QuotedEntity
#[Column(name: '`quoted-name`')]
public string $name = '';
}

#[Entity]
#[Table(name: 'sti_box')]
class STIBox
{
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public int|null $id = null;
}

#[Entity]
#[Table(name: 'sti_location')]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorColumn(name: 'type', type: 'string')]
#[DiscriminatorMap(['box_container' => STIBoxContainer::class, 'packed_box_container' => STIPackedBoxContainer::class])]
abstract class STILocation
{
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public int|null $id = null;
}

#[Entity]
class STIBoxContainer extends STILocation
{
#[ManyToOne(targetEntity: STIBox::class)]
#[JoinColumn(name: 'box_id', referencedColumnName: 'id')]
public STIBox|null $box = null;
}

#[Entity]
class STIPackedBoxContainer extends STILocation
{
#[ManyToOne(targetEntity: STIBox::class)]
#[JoinColumn(name: 'box_id', referencedColumnName: 'id')]
public STIBox|null $box = null;
}
Loading