Skip to content

Commit ab5e9e3

Browse files
rmotalnderrabus
andauthored
Fix SchemaTool::getSchemaFromMetadata() uniqueConstraint without a predefined name (#11314)
* Fix loading SchemaTool::getSchemaFromMetadata() uniqueConstraint without a name Fixes a type miss-match exception when reading a UniqueConstraint defined on an Entity which doesn't have a predefined name. * Fix deprecation on DBAL 3 --------- Co-authored-by: Alexander M. Turek <[email protected]>
1 parent 52a6a21 commit ab5e9e3

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Tools/SchemaTool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static function (ClassMetadata $class) use ($idMapping): bool {
365365

366366
if (isset($class->table['uniqueConstraints'])) {
367367
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
368-
$uniqIndex = new Index($indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
368+
$uniqIndex = new Index('tmp__' . $indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
369369

370370
foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
371371
$method = method_exists($tableIndex, 'isFulfilledBy') ? 'isFulfilledBy' : 'isFullfilledBy';

tests/Tests/ORM/Tools/SchemaToolTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,27 @@ public function testConfigurationSchemaIgnoredEntity(): void
374374
self::assertTrue($schema->hasTable('first_entity'), 'Table first_entity should exist.');
375375
self::assertFalse($schema->hasTable('second_entity'), 'Table second_entity should not exist.');
376376
}
377+
378+
#[Group('11314')]
379+
public function testLoadUniqueConstraintWithoutName(): void
380+
{
381+
$em = $this->getTestEntityManager();
382+
$entity = $em->getClassMetadata(GH11314Entity::class);
383+
384+
$schemaTool = new SchemaTool($em);
385+
$schema = $schemaTool->getSchemaFromMetadata([$entity]);
386+
387+
self::assertTrue($schema->hasTable('GH11314Entity'));
388+
389+
$tableEntity = $schema->getTable('GH11314Entity');
390+
391+
self::assertTrue($tableEntity->hasIndex('uniq_2d81a3ed5bf54558875f7fd5'));
392+
393+
$tableIndex = $tableEntity->getIndex('uniq_2d81a3ed5bf54558875f7fd5');
394+
395+
self::assertTrue($tableIndex->isUnique());
396+
self::assertSame(['field', 'anotherField'], $tableIndex->getColumns());
397+
}
377398
}
378399

379400
/**
@@ -559,6 +580,32 @@ class IndexByFieldEntity
559580
public $fieldName;
560581
}
561582

583+
/**
584+
* @Entity
585+
* @Table(uniqueConstraints={@UniqueConstraint(columns={"field", "anotherField"})})
586+
*/
587+
class GH11314Entity
588+
{
589+
/**
590+
* @Column(type="integer")
591+
* @Id
592+
* @var int
593+
*/
594+
private $id;
595+
596+
/**
597+
* @Column(name="field", type="string")
598+
* @var string
599+
*/
600+
private $field;
601+
602+
/**
603+
* @Column(name="anotherField", type="string")
604+
* @var string
605+
*/
606+
private $anotherField;
607+
}
608+
562609
class IncorrectIndexByFieldEntity
563610
{
564611
/** @var int */

0 commit comments

Comments
 (0)