Skip to content

Commit

Permalink
distinct() updates QueryBuilder state correctly
Browse files Browse the repository at this point in the history
Previously calling distinct() when the QueryBuilder was in clean state would cause subsequent getDQL() calls to ignore the distinct queryPart

Fixes #10784
  • Loading branch information
daniel-innosabi authored and greg0ire committed Jun 25, 2023
1 parent f76bab2 commit efb50b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,12 @@ public function select($select = null)
*/
public function distinct($flag = true)
{
$this->dqlParts['distinct'] = (bool) $flag;
$flag = (bool) $flag;

if ($this->dqlParts['distinct'] !== $flag) {
$this->dqlParts['distinct'] = $flag;
$this->state = self::STATE_DIRTY;
}

return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,18 @@ public function testAddDistinct(): void
self::assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
}

public function testDistinctUpdatesState(): void
{
$qb = $this->entityManager->createQueryBuilder()
->select('u')
->from(CmsUser::class, 'u');

$qb->getDQL();
$qb->distinct();

self::assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
}

/** @group DDC-2192 */
public function testWhereAppend(): void
{
Expand Down

0 comments on commit efb50b9

Please sign in to comment.