Transforming SQL into QueryBuilder #8856
-
Hello, I have an SQL query that I need to do in QueryBuilder, but I can't get it because of multiple leftJoin. Here is my SQL query SELECT r.lot,DATE_FORMAT(r.datetime_creation,'%d-%m-%Y %H:%i') as date_creation, u.societe, j_3.date_cloture , COUNT(r.id_reparation) as nb_element, j_2.nb_element_total
, IF(j_1.nb_element_cloture IS NULL,0,CONVERT((j_1.nb_element_cloture * 100 / j_2.nb_element_total),int)) as progression
FROM utilisateur u, reparation r
LEFT JOIN (SELECT COUNT(*) as nb_element_cloture, lot FROM reparation WHERE fk_reparation_statut_id IN (5,10,11,14,15,23,24,27) GROUP BY lot) j_1 ON j_1.lot = r.lot
LEFT JOIN (SELECT COUNT(*) as nb_element_total, lot FROM reparation WHERE fk_reparation_statut_id > 0 GROUP BY lot) j_2 ON j_2.lot = r.lot
LEFT JOIN (SELECT DATE_FOreparationT(datetime_cloture ,'%d-%m-%Y %H:%i') as date_cloture,lot
FROM reparation
WHERE fk_reparation_statut_id IN (5,10,11,14,15,23,24,27)
GROUP BY lot
ORDER BY datetime_cloture DESC) j_3 ON j_3.lot = r.lot
WHERE r.fk_reparation_statut_id IN(1, 16)
AND r.revendeur = u.id_utilisateur Is it possible to put the SELECT in the leftJoin with QueryBuilder? and if so how should I proceed? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Are you sure you need to use a query builder? Should any part of your SQL query become optional? If not, I would recommend against using a query builder. Here is a table to help you take that kind of decision: https://greg0ire.fr/doctrine-autrement/#/32 (:fr:) If you actually need a query builder here, I think you might be able to achieve it with the SQL (as opposed to DQL, which I doubt you actually need here) query builder, from the DBAL package ( |
Beta Was this translation helpful? Give feedback.
Are you sure you need to use a query builder? Should any part of your SQL query become optional? If not, I would recommend against using a query builder. Here is a table to help you take that kind of decision: https://greg0ire.fr/doctrine-autrement/#/32 (:fr:)
If you actually need a query builder here, I think you might be able to achieve it with the SQL (as opposed to DQL, which I doubt you actually need here) query builder, from the DBAL package (
Doctrine\DBAL\Query\QueryBuilder
).