8

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • let me get this straight. you want to remove the deleted payment methods from the list in Store class? deleting it from the DB won't remove it from the list. – jere Feb 10 '12 at 15:06
  • If it is a many to many, can you not just set the `$stores` collection in `PaymentMethod` to an empty collection and persist it? You would need to invalidate all `Store` objects in memory to ensure that their reciprocal links did not keep the records. Though in an ideal world, the accessors methods you write to perform the empty would go through the `Stores` removing the reciprocal links. – Orbling Feb 10 '12 at 15:48

1 Answers1

-3

Something along these lines ?

$qb = $em->createQueryBuilder();

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

return $qb->getQuery()->getResult();
briangallagher
  • 539
  • 4
  • 15
  • 2
    StorePaymentMethod is not an entity, that's just the join table. So in DQL it has no meaning unfortunately! – BenMorel Nov 22 '12 at 19:34