如何批量删除Doctrine 2中ManyToMany关系中的实体?

Ben*_*min 8 php many-to-many dql doctrine-orm

说我有以下课程:

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

class PaymentMethod
{
}
Run Code Online (Sandbox Code Playgroud)

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

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

DELETE FROM StorePaymentMethod WHERE paymentMethodId = ?
Run Code Online (Sandbox Code Playgroud)

有没有办法在Doctrine中做到这一点,最好是在DQL中?

bri*_*her -3

沿着这些思路的东西?

$qb = $em->createQueryBuilder();

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

return $qb->getQuery()->getResult();
Run Code Online (Sandbox Code Playgroud)

  • StorePaymentMethod 不是一个实体,它只是连接表。所以不幸的是,在 DQL 中它没有任何意义! (2认同)