I have a normal many to many relationship between two entities : User and Object.
________ _________________ ____________
| User | | User_Object | | Object |
|------| |---------------| |----------|
| id | | user_id | | id |
| .... | | object_id | | .... |
|______| |_______________| |__________|
I want to batch delete pretty big sets of Users (and the records associated with them in the User_Object table). Removing the entities one by one isn't fast enough for my needs (for > 1000 entities it takes a loooonnnng time).
//This method is far too slow for my needs
$qb = $this->doctrine->em->createQueryBuilder();
$qb->select('u')
->from('Entities\User', 'u')
->where("u.whatever= ?1")
->setParameter(1, $whatever);
$users = $qb->getQuery()->getResult();
foreach($users as $user)
{
$this->doctrine->em->remove($user);
//...
The doctrine docs say that the most efficient to bulk delete entities is DQL, which would give me something like :
$qb = $this->doctrine->em->createQuery('delete from Entities\User u where u.whatever = ?1');
$qb->setParameter(1, $whatever);
$numDeleted = $qb->execute(); //This will throw because of User_Object records
This will throw an exception because of the records in the User_Object join table (referential integrity exception).
So, my question is : how do I delete the records in the join table efficiently in a bulk delete scenario.
I would really like to avoid throwing raw SQL at it, the rest of my code uses entities everywhere and I would like to keep it that way if at all possible.
EDIT : The relationship is marked as such (I use yml) :
manyToMany:
objects:
targetEntity: Object
inversedBy: users
cascade: ["remove"]
joinTable:
name: User_Object
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
object_id:
referencedColumnName: id