而不是用
一个一个地删除我的实体$this->em->remove($price);
我想执行原生 SQL 查询来删除我的所有实体。
这是我尝试过的:
$sqlQuery = "delete from mytable where mytable.fieldone_id = ".$fieldoneid." and mytable.fieldtwo_id = ".$fieldtwoid.";";
$query = $this->getEntityManager()->createNativeQuery($sqlQuery);
$query->execute();
它返回以下错误:
Catchable fatal error: Argument 2 passed to Doctrine\ORM\EntityManager::createNativeQuery() must be an instance of Doctrine\ORM\Query\ResultSetMapping, none given
它要我传递一个ResultSetMapping
,但它是一个删除查询...
谁能教我怎么做?
最佳答案
在我看来,我使用一种不同的方式来执行 native SQL 查询,这种方式要容易得多。尝试这样的事情(我也是使用在查询中包含变量的 PDO 方法,这样更安全):
$sql = "delete from mytable where mytable.fieldone_id = :fieldoneid and mytable.fieldtwo_id = :fieldtwoid";
$params = array('fieldoneid'=>$fieldoneid, 'fieldtwoid'=>$fieldtwoid);
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute($params);
// if you are doing a select query, fetch the results like this:
// $result = $stmt->fetchAll();
这对我很有用,希望对你有帮助
关于php - Symfony 2 - Doctrine 2 - Native Sql - 删除查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000615/