jhondoe500 jhondoe500 - 5 months ago 30
PHP Question

symfony2 sql query to queryBuilder or createQuery

How can I write my SQL query in Symfony2's query builder?

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


Doesn't work any possibilities which I've tried.

Answer

You can try using:

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

if you have just a static query.

Or if you have user inputs to validate use:

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