How to remove all associations between entities using QueryBuilder #6721

Open
opened 2026-01-22 15:37:33 +01:00 by admin · 0 comments
Owner

Originally created by @Aerendir on GitHub (May 15, 2021).

Forgive me if I use the issues to get support, but on StackOverflow no one replied me :(

I have an entity Transaction.

It has two properties to maintain some self many-to-many bidirectional associations:

class Transaction
{
    /**
     * This is the owning side as we set the relation when the currency is swapped.
     *
     * @ORM\ManyToMany(targetEntity=Transaction::class, inversedBy="swappedBy")
     * @ORM\JoinTable(name="transactions_swaps",
     *      joinColumns={@ORM\JoinColumn(name="bought_in_transaction", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="sold_by_transaction", referencedColumnName="id")}
     * )
     */
    private Collection $swaps;

    /**
     * @ORM\ManyToMany(targetEntity=Transaction::class, mappedBy="swaps")
     */
    private Collection $swappedBy;
}

How to remove all the associations using the QueryBuilder?

I need to do something like this:

    public function resetSwaps(Account $account):void
    {
        
            $queryBuilder = $this->_em->createQueryBuilder();
            $query = $queryBuilder
                ->delete(Transaction::class, 't')
                ->join('t.swaps', 'swaps')
                ->where($queryBuilder->expr()->eq('t.account', ':account'))
                ->setParameter('account', $account)
                ->getQuery();

            $query->execute();
    }

But this obviously doesn't work because what I'm actually deleting is the Transaction and not the relation.

And in fact, I get this error in return:

[PDOException (23503)]
SQLSTATE[23503]: Foreign key violation: 7 ERROR: update or delete on table "transactions" violates foreign key constraint "fk_8af2a831c6351790" on table "transactions_swaps"
DETAIL: Key (id)=(17) is still referenced from table "transactions_swaps".

How to remove all the relations using the QueryBuilder?

Originally created by @Aerendir on GitHub (May 15, 2021). Forgive me if I use the issues to get support, but [on StackOverflow](https://stackoverflow.com/q/67469107/1399706) no one replied me :( I have an entity `Transaction`. It has two properties to maintain some self `many-to-many` bidirectional associations: ```php class Transaction { /** * This is the owning side as we set the relation when the currency is swapped. * * @ORM\ManyToMany(targetEntity=Transaction::class, inversedBy="swappedBy") * @ORM\JoinTable(name="transactions_swaps", * joinColumns={@ORM\JoinColumn(name="bought_in_transaction", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="sold_by_transaction", referencedColumnName="id")} * ) */ private Collection $swaps; /** * @ORM\ManyToMany(targetEntity=Transaction::class, mappedBy="swaps") */ private Collection $swappedBy; } ``` How to remove all the associations using the QueryBuilder? I need to do something like this: ```php public function resetSwaps(Account $account):void { $queryBuilder = $this->_em->createQueryBuilder(); $query = $queryBuilder ->delete(Transaction::class, 't') ->join('t.swaps', 'swaps') ->where($queryBuilder->expr()->eq('t.account', ':account')) ->setParameter('account', $account) ->getQuery(); $query->execute(); } ``` But this obviously doesn't work because what I'm actually deleting is the `Transaction` and not the relation. And in fact, I get this error in return: > [PDOException (23503)] > SQLSTATE[23503]: Foreign key violation: 7 ERROR: update or delete on table "transactions" violates foreign key constraint "fk_8af2a831c6351790" on table "transactions_swaps" > DETAIL: Key (id)=(17) is still referenced from table "transactions_swaps". **How to remove all the relations using the QueryBuilder?**
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6721