symfony2 Doctrine delete an array of objects

doctrine, php, symfony

Solution

Why don't you just loop through the objects array?

$user_services = $em->getRepository('ProjectTestBundle:UserService')
->findByUser($this->getUser()->getId());

foreach ($user_services as $user_service) {
    $em->remove($user_service);
}

$em->flush();

Problem

I would like to delete all the records from database matching a particular user_id in Symfony2. ``` $em = $this->getDoctrine()->getManager(); $user_service = $em->getRepository('ProjectTestBundle:UserService') ->findByUser($this->getUser()->getId()); ``` This might return a few matching objects, so when I run: ``` $em->remove($user_service); $em->flush(); ``` an error occurs: ``` EntityManager#remove() expects parameter 1 to be an entity object, array given. ``` How do I remove all records (objects) matching a particular condition? Btw, when I run an equivalent sql statement in mysql, it works perfectly.

Original source