Doctrine / Symfony2 - DELETE FROM表WHERE field =“test”

时间:2013-07-28 17:44:49

标签: symfony doctrine-orm doctrine

我无法相信我似乎无法在任何地方找到它(不仅仅是强制整个SQL)。文档谈论添加,搜索,更新和删除实体,但我无法看到如何在纯SQL中执行此操作:

DELETE FROM table WHERE field = "test"

我猜这只是添加和更新,所以我不能使用它:

$product = new Product();

// etc.

$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();

我认为'remove'选项也不会这样做:

$em->remove($product);
$em->flush();

那么,有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:7)

搜索实体并将其删除。

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('MyBundle:Product');

/** @var $product Product */
$product = $repository->findOneBy(array('field' => 'test'));
$em->remove($product);
$em->flush();

答案 1 :(得分:5)

您可以像这样创建DQL查询:

$query = $repository->createQuery('DELETE FROM entity e WHERE e.id = :id');
$query->setParameter('id', $id);
$query->execute();