Is there a way to add a LIMIT to an UPDATE query in a Doctrine ORM?

I am using Doctrine 2.5.x and I'm having trouble getting a LIMIT clause to work with UPDATE queries. It always updates all consistent records (i.e., it seems to ignore the LIMIT clause).

setMaxResults() seems to have no effect when used with UPDATE queries.

As a quick workaround, I use my own MySQL query, but this may not be the best solution.

I tried these examples, but no one works:

Doctrine Update Request with LIMIT

https://recalll.co/app/?q=doctrine2%20-%20Doctrine%20update%20query%20with%20LIMIT

QueryBuilder with setMaxResults()(not working):

$qb = $em->createQueryBuilder();

$query = $qb->update('\Task\Entity', 't')
    ->set('t.ClaimedBy', 1)
    ->where('t.Claimed IS NULL')
    ->getQuery();
$query->setMaxResults(20);

$this->log($query->getSQL());

Hope someone can help find a better solution than your own request. This takes away all the benefits of ORM.

LIMIT UPDATE?

+4
2

, , SQL UPDATE ... LIMIT ..., ORM, , .


MySQL, , UPDATE ... LIMIT ... SQL:

MySQL Server , , , SQL. , , SQL-. , MySQL, , :

  • SQL
    • ORDER BY LIMIT UPDATE DELETE.

, , , , SQL, ORM , , .


, , , DQL, :

Ocramius 2 2014
DQL UPDATE, .

DoctrineBundle ( ORM).

, ISO, , , :

ISO UPDATE LIMIT UPDATE, SELECT - , , , .

, , ORM - SQL, . , , ORM .

, SQL, MYSQL, SELECT:

select * from demo limit 10

SQL Server

select top 10 from demo

Orcale

select * from demo WHERE rownum = 1

.: fooobar.com/questions/565106/...

+1

, Doctrine, LIMIT UPDATE ( MySQL).

, - , . ORM.

, - ( ORM , ).

: -. . 20 ( ?).

, - , .

class TaskService
{

    private $taskRepository;

    public function __construct(TaskRepository $taskRepository)
    {
        $this->taskRepository = $taskRepository;
    }

    public function updateClaimedBy()
    {
        $criteria = ['Claimed' => null];
        $orderBy = null;

        // Only update the first 20 because XYZ
        $limit = 20;

        $tasks = $taskRepository->findBy($criteria, $orderBy, $limit);

        foreach($tasks as $task) {
            $task->setClaimedBy(1)
        }
    }

}
+1

Source: https://habr.com/ru/post/1678027/


All Articles