使用 Doctrine 在 MySQL 数据库上进行多表删除的语法是什么?

发布于 2024-08-21 09:27:33 字数 1096 浏览 6 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2024-08-28 09:27:33

解决了。我没有尝试自己构建连接,而是使用了子查询,并且没有尝试强制 Doctrine 以特定方式构建 SQL。

对于任何其他需要做这样的事情的人来说,这就是最终有效的方法:

$query = Doctrine_Query::create()
  ->delete('Company_group')
  ->where( "companyID = ?", array( $companyID ) )
  ->addWhere( "groupID IN (SELECT g.id FROM Group g WHERE g.groupType = 'public')" );

干杯。

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:

$query = Doctrine_Query::create()
  ->delete('Company_group')
  ->where( "companyID = ?", array( $companyID ) )
  ->addWhere( "groupID IN (SELECT g.id FROM Group g WHERE g.groupType = 'public')" );

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文