Put12co22mer2 Put12co22mer2 - 1 year ago 536
PHP Question

Symfony 2 - Doctrine 2 - Native Sql - Delete Query

Instead of removing my entities one by one with


I would like to execute a native SQL query to delete all my entities.

Here is what I tried :

$sqlQuery = "delete from mytable where mytable.fieldone_id = ".$fieldoneid." and mytable.fieldtwo_id = ".$fieldtwoid.";";

$query = $this->getEntityManager()->createNativeQuery($sqlQuery);


It returns the following error :

Catchable fatal error: Argument 2 passed to Doctrine\ORM\EntityManager::createNativeQuery() must be an instance of Doctrine\ORM\Query\ResultSetMapping, none given

It wants me to pass a
, but it is a delete query...

Can anyone please teach me how to do it?

Answer Source

I use a different way of executing native SQL queries that is much easier, in my opinion. Try something like this (I am also using the PDO method of including variables in the query, which is safer):

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

This works great for me, hope it helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download