我需要将SQL本机放入查询生成器Doctrine2中


I need to put SQL native in query Builder Doctrine2

我需要在查询生成器原则2中使用SQL NATIVE来使用SQL函数(CONCAT、REPLACE、LDAP)。请帮帮我。

您可以尝试:

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

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

这是针对条令2 ORM。表myTable可以按bundle、按类路径+类名或按名称空间+类名(... FROM My'Namespace'Class'Model tbl ...)寻址。