18

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.

4 Answers 4

30

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();
5
  • no problem, sometimes you don't see the forest for the trees... happens :P Feb 10, 2014 at 12:38
  • Indeed :)) I tried while/for loops before; forgot about foreach for a moment.
    – Davit
    Feb 10, 2014 at 12:41
  • 3
    Thanks for the solution, i was thinking there is a solution in Doctrine style. :) Jan 29, 2016 at 13:08
  • As @zennin, I was expecting for a Doctrine native solution. Althought I didn't want to use the loop, I think it will do the trick ;D
    – Drumnbass
    Jun 22, 2016 at 9:17
  • Don't loop when working with databases. This will send a single statement for every element to the database. It's ok with 3, but not ok with 1000. Jan 27, 2023 at 10:29
6

You can also make use of doctrine query builder delete method.

public function deleteAllByUser(UserInterface $user)
{
    $query = $this->createQueryBuilder('related_entity')
        ->delete()
        ->andWhere('related_entity.user = :user')
        ->setParameter('user', $user)
        ->getQuery();

    return $query->execute();
}
1
  • Probably the fastest when working with many objects in comparison to the other solutions. I do wonder why an array as a parameter in the "$em->remove()/persist()" methods is not accepted. But there must be a good reason for it!
    – Manuel
    May 26, 2023 at 8:35
5

You could also use something like this:

$user_services = $em->getRepository('ProjectTestBundle:UserService')->findByUser($this->getUser()->getId());
array_walk($user_services, array($this, 'deleteEntity'), $em);
$em->flush();

Then add this method in your controller:

protected function deleteEntity($entity, $key, $em)
{
    $em->remove(entity);
}

Or simply use:

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

...

protected function deleteEntities($em, $entities)
{
    foreach ($entities as $entity) {
        $em->remove($entity);
    }
}

Note that when using Propel and the PropelBundle, the PropelObjectCollection implements a delete() function so you don't have to do this loop by hand.

2
  • For the first method, couldn't you just do array_walk($user_services, array($em, 'remove')); ? Jun 2, 2017 at 22:12
  • Yes indeed, but in the deleteEntities method, so you don't have to rewrite this code each time.
    – COil
    Jun 5, 2017 at 11:00
-1

If you return one object, you just need to write:

 ->findOneByUser,

You won't need a foreach loop. If your return is an array, then you need to do ->findByUser and write a for loop:

foreach ($entities as $entity)
{
  //do something
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.