Skip to content

Commit

Permalink
Using an integer as discriminator value with ORM v3
Browse files Browse the repository at this point in the history
This fixes a bug that occurred when configuring integers as discriminator values and using DQL instanceOf function in the queries. Doctrine throws a type error whenever the application generates these queries.
  • Loading branch information
prohalexey committed May 15, 2024
1 parent 3d9af31 commit bc1aeb8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2252,7 +2252,7 @@ private function getChildDiscriminatorsFromClassMetadata(
}

foreach (array_keys($discriminators) as $dis) {
$sqlParameterList[] = $this->conn->quote($dis);
$sqlParameterList[] = is_int($dis) ? $dis : $this->conn->quote((string) $dis);

Check failure on line 2255 in src/Query/SqlWalker.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

RedundantCastGivenDocblockType

src/Query/SqlWalker.php:2255:76: RedundantCastGivenDocblockType: Redundant cast to string given docblock-provided type (see https://psalm.dev/263)

Check failure on line 2255 in src/Query/SqlWalker.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

RedundantCastGivenDocblockType

src/Query/SqlWalker.php:2255:76: RedundantCastGivenDocblockType: Redundant cast to string given docblock-provided type (see https://psalm.dev/263)
}

return '(' . implode(', ', $sqlParameterList) . ')';
Expand Down
22 changes: 22 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11341Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ public function testDiscriminatorValue(string $dql, string $expectedDiscriminato

self::assertMatchesRegularExpression($expectedDiscriminatorValues, $sql);
}

public static function dqlStatementsForInstanceOf(): Generator
{
yield [IntegerBaseClass::class, IntegerFooEntity::class];
yield [StringBaseClass::class, StringFooEntity::class];
yield [StringAsIntBaseClass::class, StringAsIntFooEntity::class];
}

#[DataProvider('dqlStatementsForInstanceOf')]
public function testInstanceOf(string $baseClass, string $inheritedClass): void
{
$this->_em->persist(new $inheritedClass());
$this->_em->flush();

$dql = "SELECT p FROM {$baseClass} p WHERE p INSTANCE OF {$baseClass}";

Check failure on line 66 in tests/Tests/ORM/Functional/Ticket/GH11341Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Variable "$baseClass" not allowed in double quoted string; use concatenation instead

Check failure on line 66 in tests/Tests/ORM/Functional/Ticket/GH11341Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Variable "$baseClass" not allowed in double quoted string; use concatenation instead

$query = $this->_em->createQuery($dql);
$result = $query->getResult();

self::assertCount(1, $result);
self::assertContainsOnlyInstancesOf($baseClass, $result);
}
}

#[ORM\Entity]
Expand Down

0 comments on commit bc1aeb8

Please sign in to comment.