I need to put SQL native in query Builder Doctrine2 I need to put SQL native in query Builder Doctrine2 symfony symfony

I need to put SQL native in query Builder Doctrine2


You may try :

$connection = $this->get('doctrine')->getConnection();$toto = "toto";$foo = "foo";$params = array('param1' => $toto, 'param2' => $foo);$request = "SELECT * FROM table WHERE param1 = :param1 AND param2 = :param2";try {  $stmt = $connection->executeQuery($request, $params);} catch (\PDOException $e) {  // echo $e->getMessage();}while (($result = $stmt->fetch(\PDO::FETCH_ASSOC))) {  // stuff with $result}

If you want to do such a request on a service, you may need :

use Doctrine\DBAL\Connection;


Supposing You have an entity manager stored in $this->em:

$dql = $this->em->createQuery('    SELECT CONCAT(tbl.col1, ' ', tbl.col2), COALESCE(SUM(tbl.col3), 0)    FROM myTable tbl');$result = $dql->getResult();print_r($result);

This is for Doctrine 2 ORM. The table myTable can be addresed by bundle, by class path + class name or by namespace + class name (... FROM My\Namespace\Class\Model tbl ...).