Skip to content

Commit 5c50ed9

Browse files
authored
Merge pull request #11924 from xabbuh/dbal-6867
prefer primary key constraints over Index::isPrimary()
2 parents 78e8887 + 5a1e560 commit 5c50ed9

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ parameters:
13951395
-
13961396
message: '#^Call to method toString\(\) on an unknown class Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
13971397
identifier: class.notFound
1398-
count: 4
1398+
count: 5
13991399
path: src/Mapping/Driver/DatabaseDriver.php
14001400

14011401
-
@@ -1473,7 +1473,7 @@ parameters:
14731473
-
14741474
message: '#^Parameter \$name of anonymous function has invalid type Doctrine\\DBAL\\Schema\\Name\\UnqualifiedName\.$#'
14751475
identifier: class.notFound
1476-
count: 4
1476+
count: 5
14771477
path: src/Mapping/Driver/DatabaseDriver.php
14781478

14791479
-

src/Mapping/Driver/DatabaseDriver.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,13 @@ private function reverseEngineerMappingFromDatabase(): void
319319
*/
320320
private function buildIndexes(ClassMetadata $metadata): void
321321
{
322-
$tableName = $metadata->table['name'];
323-
$indexes = $this->tables[$tableName]->getIndexes();
322+
$tableName = $metadata->table['name'];
323+
$table = $this->tables[$tableName];
324+
$primaryKey = self::getPrimaryKey($table);
325+
$indexes = $table->getIndexes();
324326

325327
foreach ($indexes as $index) {
326-
if ($index->isPrimary()) {
328+
if ($index === $primaryKey) {
327329
continue;
328330
}
329331

@@ -506,7 +508,7 @@ private function buildToOneAssociationMappings(ClassMetadata $metadata): void
506508
private function getTablePrimaryKeys(Table $table): array
507509
{
508510
try {
509-
if (method_exists($table, 'getPrimaryKeyConstraints')) {
511+
if (method_exists($table, 'getPrimaryKeyConstraint')) {
510512
return array_map(static fn (UnqualifiedName $name) => $name->toString(), $table->getPrimaryKeyConstraint()->getColumnNames());
511513
}
512514

@@ -594,4 +596,27 @@ private static function getIndexedColumns(Index $index): array
594596

595597
return $index->getColumns();
596598
}
599+
600+
private static function getPrimaryKey(Table $table): Index|null
601+
{
602+
$primaryKeyConstraint = null;
603+
604+
if (method_exists(Table::class, 'getPrimaryKeyConstraint')) {
605+
$primaryKeyConstraint = $table->getPrimaryKeyConstraint();
606+
}
607+
608+
foreach ($table->getIndexes() as $index) {
609+
if ($primaryKeyConstraint !== null) {
610+
$primaryKeyConstraintColumns = array_map(static fn (UnqualifiedName $name) => $name->toString(), $primaryKeyConstraint->getColumnNames());
611+
612+
if ($primaryKeyConstraintColumns === self::getIndexedColumns($index)) {
613+
return $index;
614+
}
615+
} elseif ($index->isPrimary()) {
616+
return $index;
617+
}
618+
}
619+
620+
return null;
621+
}
597622
}

0 commit comments

Comments
 (0)