Younes
Younes

Reputation: 31

I need to put SQL native in query Builder Doctrine2

i need to work with SQL NATIVE in query Builder doctrine 2 for using SQL Function (CONCAT,REPLACE,LDAP) . please Help me.

Upvotes: 3

Views: 7627

Answers (2)

shadyyx
shadyyx

Reputation: 16055

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 ...).

Upvotes: 0

Alain
Alain

Reputation: 36964

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;

Upvotes: 3

Related Questions