Doctrine QueryBuilder delete with joins(Doctrine QueryBuilder 删除连接)
问题描述
我正在尝试使用 Doctrine QueryBuilder 来执行以下 SQL 查询:
I'm trying to use the Doctrine QueryBuilder to perform the following SQL query:
DELETE php FROM product_hole_pattern php
INNER JOIN hole_pattern hp ON php.hole_pattern_id = hp.id
INNER JOIN hole_pattern_type hpt ON hp.hole_pattern_type_id = hpt.id
WHERE php.product_id = 4 AND hpt.slug='universal';
我有这个
$qb = $this->entityManager->createQueryBuilder();
$query = $qb->delete('SANUSEntityProductHolePattern', 'php')
->innerJoin('php.holePattern', 'hp')
->innerJoin('hp.holePatternType', 'hpt')
->where('hpt.slug = :slug AND php.product=:product')
->setParameter('slug','universal')
->setParameter('product',$this->id)
->getQuery();
但我明白了:
[Semantical Error] line 0, col 50 near 'hpt.slug = :slug': Error: 'hpt' is not defined.
错误信息自带的DQL是:
The DQL that comes with the error message is:
DELETE SANUSEntityProductHolePattern php
WHERE hpt.slug = :slug AND php.product=:product
所以连接似乎被完全省略了.
So the joins seem to be omitted completely.
推荐答案
使用 IN 条件运行查询可能比迭代更好.
It may be better to run a query with IN condition rather than iterating.
$ids = $this->createQueryBuilder('product')
->join('..your joins..')
->where('..your wheres..')
->select('product.id')
->getQuery()->getResult();
$this->createQueryBuilder('product')
->where('product.id in (:ids)')
->setParameter('ids', $ids)
->delete()
->getQuery()
->execute();
- 优点:运行速度更快,无需迭代
- 缺点:你不能挂钩 preRemove
至于把它放在哪里"的激烈争论,如果你愿意,敢于把它放在控制器中.这完全取决于你.但是,如果您将代码放在专用的学说存储库类中,那么将来可能对您更有用.它应该很容易做到,并且易于更改/维护.
As to heated "where to put it" debate, dare to put it in the controller if you like. That's completely up to you. However, it may be more useful to you in the future if you land the code in the dedicated doctrine repository class. It should be very easy to do and makes it easy to change / maintain.
这篇关于Doctrine QueryBuilder 删除连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine QueryBuilder 删除连接


- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01