如何在 Doctrine 2 中批量删除多对多关系中的实体?

发布于 2025-01-04 11:27:38 字数 495 浏览 2 评论 0原文

假设我有以下类:

class Store
{
    /**
     * @ManyToMany(targetEntity="PaymentMethod")
     */
    protected $paymentMethods;
}

class PaymentMethod
{
}

当我们删除(或只是禁用,而不是实际从数据库中删除)一个 PaymentMethod 时,我们希望这个 paymentMethod 从所有类中删除Store::$ paymentMethods 集合。

到目前为止,我们一直在联结表上使用原始 SQL 查询来实现此目的:

DELETE FROM StorePaymentMethod WHERE paymentMethodId = ?

有没有办法在 Doctrine 中(最好是在 DQL 中)执行此操作?

Say I have the following classes:

class Store
{
    /**
     * @ManyToMany(targetEntity="PaymentMethod")
     */
    protected $paymentMethods;
}

class PaymentMethod
{
}

When we delete (or just disable, without actually removing from the database) a PaymentMethod, we'd like that this paymentMethod gets removed from all the Store::$paymentMethods collections.

Up to now, we've been using raw SQL queries on the junction table for this:

DELETE FROM StorePaymentMethod WHERE paymentMethodId = ?

Is there a way to do that in Doctrine, preferably in DQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情绪 2025-01-11 11:27:38

沿着这些思路的东西?

$qb = $em->createQueryBuilder();

$qb->delete('StorePaymentMethod', 'spm')
   ->where('spm.paymentMethodId = : paymentMethodId')
   ->setParameter('paymentMethodId', $id);

return $qb->getQuery()->getResult();

Something along these lines ?

$qb = $em->createQueryBuilder();

$qb->delete('StorePaymentMethod', 'spm')
   ->where('spm.paymentMethodId = : paymentMethodId')
   ->setParameter('paymentMethodId', $id);

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