symfony2 sql查询到queryBuilder或createQuery

时间:2016-06-22 07:30:56

标签: php sql symfony union query-builder

如何在Symfony2的查询构建器中编写SQL查询?

(
 SELECT t2.`year_defence`, 'advisor_id' AS col, t2.advisor_id AS val, COUNT(*) AS total
 FROM projects t2
 GROUP BY t2.`year_defence`, t2.advisor_id
) 
UNION 
(
 SELECT t2.`year_defence`, 'type_id' AS col, t2.type_id AS val, COUNT(*) AS total
 FROM projects t2
 GROUP BY t2.`year_defence`, t2.type_id
)
UNION
(
 SELECT t2.`year_defence`, 'technology_id' AS col, t2.technology_id AS val, COUNT(*) AS total
 FROM projects t2
 GROUP BY t2.`year_defence`, t2.technology_id
)
ORDER BY 1 , 2 , 3;

我没有尝试过任何可能的尝试。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以尝试使用:

$statement = $this->entityManager->getConnection()->executeQuery($sql);
$result    = $statement->fetchAll();

如果您只有静态查询。

或者如果您有用户输入来验证使用:

$connection = $this->entityManager->getConnection();
$statement = $connection->prepare("SELECT myvalues FROM mytable WHERE id = :id");
$statement->bindValue('id', 1);
$statement->execute();
$results = $statement->fetchAll();