php - Symfony 2 - Doctrine 2 - Native Sql - 删除查询

标签 php sql database symfony doctrine-orm

而不是用

一个一个地删除我的实体
$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/

相关文章:

SQL Server 2008 使用 WHILE 循环插入

sql - 在sqlite3中合并两个表

sql - 在 VS2012 和 SSDT 中设置数据库图?

php - htaccess 重写规则不起作用

php - 从 jquery 插件中的多选组获取值

php - 在不求助于 mysql_insert_id() 的情况下,在单个查询中将 SQL 关系插入到 2 个表

database - 我们可以在 DB 中添加一列而不将其添加到 hibernate 对象吗?

php - Laravel php artisan serve 模仿 HTTPS

php - 如何解析服务器上 .html 文件中的 PHP 语法?

sql - 将平均值添加到表格