Skip to content

setMaxResults() does not apply LIMIT in QueryBuilder on delete. #7119

@rokas-reizgys-tg

Description

@rokas-reizgys-tg

Bug Report

Q A
Version 4.3.2
Previous Version if the bug is a regression x.y.z

Summary

Found an issue with setMaxResults() method with DELETE query. Regarding to Doctrine official docs, I assume that LIMIT clause is supported by doctrine and should be added when build a query with QueryBuilder. But in my case - it is not a case. When i found out that this does not limit the records - I tried adding setFirstResult(0), but it did not help. Here is my code and printed SQL query using getSQL():

Code:

        $query = $this->dbal
            ->createQueryBuilder()
            ->delete($this->table->getTableName())
            ->where('created_at < :retention_date')
            ->andWhere('status = :status')
            ->setParameter('retention_date', $retentionDate, Types::DATETIME_IMMUTABLE)
            ->setParameter('status', MessageStatus::HANDLED->value)
            ->setFirstResult( 0 )
            ->setMaxResults(50)
        ;

Generated query:

DELETE FROM messenger_messages_outbox_storage WHERE (created_at < ?) AND (status = ?)

This code is expected to delete 50 records with LIMIT clause applied.
Expected query:

DELETE FROM messenger_messages_outbox_storage 
WHERE (created_at < ?) AND (status = ?) 
LIMIT 50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions