我需要用几组值更新大量实体(~100k)。为此,我想使用 DQL update Query .
问题来 self 的查询的 where 子句。我需要过滤要更新的实体。
$qb->update('MyBundle/Entity/MyEntity', 'e')
->set('e.fieldToUpdate', ':value')
->innerJoin('e.relation', 'r')
->where('r.filter < :filter')
->setParameters...
不幸的是,更新和删除查询不支持连接。
而且我将无法在 e.id IN "subquery"
中使用 where 子句,因为 MySQL 文档说:
In MySQL, you cannot modify a table and select from the same table in a subquery.
出于性能原因,我想避免使用 Query#iterate()
工具或其他基于循环的解决方案,所以......我不知道如何处理这个问题。
这听起来像是一个常见问题,我可能会错过一些非常明显的东西......所以如果有解决方法可以做到这一点,我会很高兴阅读它!
最佳答案
感谢 Miro,我找到了一个非常明显的解决方案(当然)...
由于我无法在子查询内选择我正在更新的同一个表,因此我必须从另一个表中选择关系。
Doctrine 不允许选择类似 r.mytable
的内容,其中“mytable”是我要定位的实体,但是,有一个 DQL 函数可以执行此操作:IDENTITY
例如:
$dql = $queryBuilder
->from('Relation', 'r')
->select('IDENTITY(r.myEntity)')
->where('r.filter > :filter')
->getDQL()
;
$queryBuilder = $this
->createQueryBuilder('e')
->update('MyBundle/Entity/MyEntity', 'e')
->set('e.fieldToUpdate', ':value')
->where(
$queryBuilder->expr()->In('e.id', $dql)
)
->setParameters([
'filter' => $filter
]);
关于php - Doctrine 通过 join 批量更新 DQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37080116/