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 committed Jun 23, 2023
1 parent d831c12 commit eec49b5
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 eec49b5

Please sign in to comment.