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 aff565e
Show file tree
Hide file tree
Showing 2 changed files with 27 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($dis);
}

return '(' . implode(', ', $sqlParameterList) . ')';
Expand Down
26 changes: 26 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,32 @@ 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];
}

/**
* @psalm-param class-string $baseClass
* @psalm-param class-string $inheritedClass
*/
#[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;

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

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

#[ORM\Entity]
Expand Down

0 comments on commit aff565e

Please sign in to comment.