使用 Doctrine 在 MySQL 数据库上进行多表删除的语法是什么?
使用 Doctrine,我尝试根据数据删除单个表中的记录从多个表收集。
“companies_groups”是将“公司”链接到“组”的关联表。我想删除此表中链接到特定公司的所有记录,但限制是仅删除链接到“公共”组的“companies_groups”记录。
如果我用纯 SQL 编写它,它看起来会像这样:
DELETE companies_groups
FROM companies_groups, groups
WHERE companies_groups.companyID = 7
AND companies_groups.groupID = groups.id
AND groups.groupType = 'public'
Doctrine 中的等价物是什么?我现在已经挣扎和实验了一个小时左右。
目前我有这个:(
$query = Doctrine_Query::create()
->delete('Company_group cg')
->from('Company_group cg, Group g')
->where( "cg.companyID = ? AND g.groupType = 'public' AND g.id = cg.groupID ", array( $companyID ) );
我的 Doctrine 模型是“companies_groups”表的“Company_group”和“groups”表的“Group”)
生成此 SQL:
DELETE FROM companies_groups, groups WHERE (companyid = ? AND grouptype = 'public' AND id = groupid)
您可以看到生成的 SQL 之间缺少“companies_groups” DELETE 和 FROM,并且限定符被删除(意味着“id”将不明确)。
如果我可以提供任何有帮助的附加信息,请告诉我。
Using Doctrine, I am trying to delete records in a single table based on data gathered from multiple tables.
'companies_groups' is an association table linking 'companies' to 'groups'. I want to delete all records in this table linked to a specific company, with the restriction that only 'companies_groups' records linked to a 'public' group will be deleted.
If I were to write this in pure SQL, it would look something like this:
DELETE companies_groups
FROM companies_groups, groups
WHERE companies_groups.companyID = 7
AND companies_groups.groupID = groups.id
AND groups.groupType = 'public'
What is the equivalent in Doctrine? I have been struggling and experimenting for an hour or so now.
At the moment I have this:
$query = Doctrine_Query::create()
->delete('Company_group cg')
->from('Company_group cg, Group g')
->where( "cg.companyID = ? AND g.groupType = 'public' AND g.id = cg.groupID ", array( $companyID ) );
(My Doctrine models are 'Company_group' for the 'companies_groups' table and 'Group' for the 'groups' table)
Which produces this SQL:
DELETE FROM companies_groups, groups WHERE (companyid = ? AND grouptype = 'public' AND id = groupid)
You can see that the generated SQL is missing 'companies_groups' between DELETE and FROM, and that the qualifiers are dropped (meaning 'id' will be ambiguous).
Let me know if there is any additional info I can provide that would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。我没有尝试自己构建连接,而是使用了子查询,并且没有尝试强制 Doctrine 以特定方式构建 SQL。
对于任何其他需要做这样的事情的人来说,这就是最终有效的方法:
干杯。
Solved it. Rather than try to build the join myself I used a subquery and didn't try to force Doctrine to construct the SQL in a specific way.
For anyone else that ever needs to do something like this, here is what ended up working:
Cheers.