symfony2 Doctrine删除一个对象数组

时间:2023-01-09 06:49:13

I would like to delete all the records from database matching a particular user_id in Symfony2.

我想从数据库中删除与Symfony2中特定user_id匹配的所有记录。

$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.

如何删除与特定条件匹配的所有记录(对象)?顺便说一下,当我在mysql中运行一个等效的sql语句时,它运行得很好。

3 个解决方案

#1


23  

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();

#2


3  

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.

请注意,在使用Propel和PropelBundle时,PropelObjectCollection会实现delete()函数,因此您不必手动执行此循环。

#3


-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循环。如果你的返回是一个数组,那么你需要做 - > findByUser并写一个for循环:

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

#1


23  

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();

#2


3  

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.

请注意,在使用Propel和PropelBundle时,PropelObjectCollection会实现delete()函数,因此您不必手动执行此循环。

#3


-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循环。如果你的返回是一个数组,那么你需要做 - > findByUser并写一个for循环:

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