带子查询的 Doctrine Update 查询
我正在尝试使用 Doctrine dql 执行类似于以下查询的查询:
Doctrine_Query::create()
->update('Table a')
->set('a.amount',
'(SELECT sum(b.amount) FROM Table b WHERE b.client_id = a.id AND b.regular = ? AND b.finished = ?)',
array(false, false))
->execute();
但它会引发 Doctrine_Query_Exception 并显示消息:“未知组件别名 b”
是关于在“set”子句中使用子查询的限制,您可以吗给我一些帮助吗?
提前致谢。
I'm trying to execute a query, similar to the following one, using doctrine dql:
Doctrine_Query::create()
->update('Table a')
->set('a.amount',
'(SELECT sum(b.amount) FROM Table b WHERE b.client_id = a.id AND b.regular = ? AND b.finished = ?)',
array(false, false))
->execute();
But it rises a Doctrine_Query_Exception with the message: "Unknown component alias b"
Is restriction about using sub-queries inside the 'set' clause, can you give me some help?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几年后但可能会有所帮助。
是]
如果您需要/想要/必须,您可以使用Querybuilder来执行具有子选择语句的更新查询,而不是直接使用底层连接层。
这里的想法是使用 QueryBuilder 两次。
示例]
给定一个用户可以出售物品的应用程序。每笔交易都涉及买方和卖方。交易结束后,卖家和买家可以留下评论,了解对方的交易进展情况。
您可能需要一个用户表、一个审核表和一个交易表。
用户表包含一个名为评级的字段,它将保存用户的平均评级。 Review 表存储事务 ID、作者 ID(提交评论的人)、值(从 0 到 5)。最后,交易包含对卖方和买方的参考。
现在假设您想在对方提交评论后更新用户的平均评分。更新查询将计算用户的平均评分,并将结果作为
User. rating
属性的值。我将以下代码片段与 Doctrine 2.5 和 Symfony3 结合使用。由于这项工作是关于用户的,因此我在 AppBundle\Entity\UserRepository.php 存储库中创建一个名为
updateRating( User $user)
的新公共函数是有意义的。现在从控制器
Years later but may help.
Yes ]
If you need/want/have to, you can use the Querybuilder to execute an update query having a sub select statement, instead of using directly the underlying connection layer.
The idea here is to use the QueryBuilder twice.
Example ]
Given an application where users can sell objects. Each transaction involves a buyer and a seller. After a transaction ends, sellers and buyers can leave a review on how went the deal with their counter part.
You might need a User table, a Review table and a Transaction table.
The User table contains a field named rating which will hold the average rating for a user. The Review table stores a transaction id, the author id (who submitted the review), a value (from 0 to 5). Finally, the transaction contains a reference for both the seller and the buyer.
Now let's say you would like to update the average rating for a user after a review has been submitted by the counter part. The update query will compute the average rating for a user and put the result as the value of the
User.rating
property.I used the following snippet with Doctrine 2.5 and Symfony3. Since the work is about users, I makes sense to create a new public function called
updateRating( User $user)
inside the AppBundle\Entity\UserRepository.php repository.Now from the controller
我不确定这是否有限制,但我记得不久前曾与此进行过斗争。我最终让它工作起来:
看看这是否有效。请注意,此查询不会执行自动变量转义,因为它不是 DQL。
I'm not sure if there's a restriction on this but I remember fighting with this sometime ago. I eventually got it working with:
See if that does the trick. Note that automatic variable escaping doesn't get executed with this query as it's not DQL.