Doctrine Update Request Using LIMIT

I would like to make an update request using LIMIT as follows:

UPDATE anytable SET anycolumn = 'anyvalue' WHERE anothercolumn='anothervalue' LIMIT 20

How is this possible with teaching 2.1?

+2
source share
3 answers

Not a specific doctrine, but perhaps perhaps with a subquery?

UPDATE messages SET test_read=1
 WHERE id IN (
     SELECT id FROM (
         SELECT id FROM messages 
         ORDER BY date_added DESC  
         LIMIT 5, 5
     ) tmp
 );
+2
source

I found that I needed to get a connection to entityManager and call executeUpdate:

$em->getConnection()->executeUpdate(
    "UPDATE anytable SET anycolumn = 'anyvalue'
     WHERE anothercolumn='anothervalue'
     LIMIT 20");

The doctrine page of your own requests says:

DELETE, UPDATE INSERT, SQL API , , . EntityManager # getConnection() executeUpdate() .

+4

EDIT:

:

1 - DQL:

$query = $entityManager->createQuery('UPDATE Entities\User u SET u.someValue = newValue WHERE u.id = someId');

// this will add the LIMIT statement
$query->setMaxResults(20);

$query->execute();

2 - QueryBuilder:

$qb = $this->_em->createQueryBuilder();

$query = $qb->update('Entities\User', 'u')
            ->set('u.someValue', newValue)
            ->where('u.id = someId')
            ->getQuery();

// this will add the LIMIT statement
$query->setMaxResults(20);   

$query->execute();

: echo $query->getSQL();, sql,

EDIT: ( ) - Native SQL

-2

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


All Articles