2

我想用这样的 LIMIT 做一个更新查询:

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

教义 2.1 怎么可能呢?

4

3 回答 3

4

我发现我必须从 entityManager 获取连接并调用 executeUpdate:

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

关于本机查询的学说页面说:

如果要执行 DELETE、UPDATE 或 INSERT 语句,则不能使用 Native SQL API,并且可能会抛出错误。使用 EntityManager#getConnection() 访问本机数据库连接并为这些查询调用 executeUpdate() 方法。

于 2012-07-19T16:34:46.020 回答
2

不是特定于教义的,但也许可以使用子查询?

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
 );
于 2012-01-17T12:50:07.773 回答
-2

编辑:

您可以通过 2 种不同的方式进行操作:

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

编辑: 另一种选择(不强烈推荐)是使用Native SQL

于 2012-01-17T16:23:17.563 回答