I need to put SQL native in query Builder Doctrine2

一世执手 提交于 2020-01-14 19:06:31

问题


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


回答1:


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;



回答2:


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



来源:https://stackoverflow.com/questions/12436312/i-need-to-put-sql-native-in-query-builder-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!