简体   繁体   English

使用连接更新主义批量DQL

[英]Doctrine bulk DQL update with join

I need to update a large collection of entities (~100k) with a few set of values. 我需要使用几组值更新大量的实体(〜100k)。 To do this, I want to use a DQL update Query . 为此,我想使用DQL更新Query

The problem came from the where clause of my query. 问题来自我查询的where子句。 I need to filter the entities to update. 我需要过滤实体以进行更新。

$qb->update('MyBundle/Entity/MyEntity', 'e')
->set('e.fieldToUpdate', ':value')
->innerJoin('e.relation', 'r')
->where('r.filter < :filter')
->setParameters...

Unfortunatly joins are not supported on update and delete queries. 不幸的是,更新和删除查询不支持联接。 And I won't be able to use a where clause with e.id IN "subquery" since MySQL documentation says : 而且由于MySQL文档说,我将无法在e.id IN "subquery"使用where子句:

In MySQL, you cannot modify a table and select from the same table in a subquery. 在MySQL中,您无法修改表并在子查询中从同一表中选择。

I'd like to avoid using Query#iterate() facility or other solutions based on loop for performance reason, so...I don't know how to deal with this. 由于性能原因,我想避免使用Query#iterate()工具或其他基于循环的解决方案,所以...我不知道该如何处理。

It sounds like a common problem, and I may miss something very obvious...so if there is a workaround to do this, i'll be glad to read it ! 这听起来像是一个常见的问题,我可能会错过一些非常明显的问题……因此,如果有解决方法,我将很高兴阅读!

Thanks to Miro, I found a pretty obvious solution (of course)... 多亏了Miro,我找到了一个非常明显的解决方案(当然)...

Since I can't select inside the subquery the same table I'm updating, I had to select the relation, from another table. 由于无法在子查询中选择要更新的同一表,因此不得不从另一个表中选择关系。

Doctrine doesn't allow to select something like r.mytable where 'mytable` is the Entity I am targeting, but, there is a DQL function to do this : IDENTITY Doctrine不允许选择r.mytable类的r.mytable ,其中“ mytable”是我要定位的实体,但是有一个DQL函数可以执行此操作:IDENTITY

For instance : 例如 :

    $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
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM