我需要在查询Builder Doctrine2中放入SQL native

时间:2012-09-15 09:47:53

标签: php symfony doctrine-orm query-builder

我需要在查询构建器原则2中使用SQL NATIVE来使用SQL函数(CONCAT,REPLACE,LDAP)。请帮助我。

2 个答案:

答案 0 :(得分:3)

您可以尝试:

$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
}

如果您想对服务提出此类请求,则可能需要:

use Doctrine\DBAL\Connection;

答案 1 :(得分:0)

假设您在$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);

这是针对Doctrine 2 ORM的。表myTable可以通过包,类路径+类名或命名空间+类名(... FROM My\Namespace\Class\Model tbl ...)添加。