Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ public function getFirstResult(): int
*/
public function setMaxResults(int|null $maxResults): static
{
if ($this->type === QueryType::Delete || $this->type === QueryType::Update) {
throw new RuntimeException('Setting a limit is not supported for delete or update queries.');
}

$this->maxResults = $maxResults;

return $this;
Expand Down
21 changes: 21 additions & 0 deletions tests/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\TestCase;
use RuntimeException;

use function array_filter;
use function class_exists;
Expand Down Expand Up @@ -72,6 +73,26 @@ public function testDeleteSetsType(): void
$this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
}

public function testDeleteWithLimitNotSupported(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Setting a limit is not supported for delete or update queries.');

$this->entityManager->createQueryBuilder()
->delete(CmsUser::class, 'c')
->setMaxResults(1);
}

public function testUpdateWithLimitNotSupported(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Setting a limit is not supported for delete or update queries.');

$this->entityManager->createQueryBuilder()
->update(CmsUser::class, 'c')
->setMaxResults(1);
}

public function testUpdateSetsType(): void
{
$qb = $this->entityManager->createQueryBuilder()
Expand Down