Moon
Moon

Reputation: 22565

How to delete many-to-many records in Doctrine?

I am trying to delete records in many to many using Doctrine. I used code on http://www.doctrine-project.org/documentation/manual/1_2/ru/working-with-models#many-to-many-relations:deleting-a-link

when I do the first method, it just deletes UserGroup record ONLY. How do I delete User, Group, and UserGroup records at once? The second and thrid methods do not work as well.

Upvotes: 0

Views: 2662

Answers (3)

Goran Jurić
Goran Jurić

Reputation: 1839

You can use DQL to delete these records, but..

Are you sure that you want to do that? Before deleting all of them you should make sure that there is no other user using the group you are deleting and that the user you are deleting does not belong to any other groups.

Upvotes: 3

takeshin
takeshin

Reputation: 50648

Add in your schema.yml something in this mood:

...
relations:
...
     onDelete: CASCADE

or in your base model:

$this->hasMany('Group as Groups', array(
         ...
         'onDelete' => 'CASCADE'));

Upvotes: 2

Amy B
Amy B

Reputation: 17977

You can't unless the tables are set to cascade on delete.

Upvotes: 1

Related Questions