How to make a UNION query in SYMFONY

1

How could I do the following query in Symfony

SELECT *
FROM ((
SELECT SUM(o0_.qty) AS sclr_0, o0_.price AS price_1, i1_.numero AS numero_2, i1_.nombre AS nombre_3, i1_.um AS um_4, i1_.id AS id_5
FROM ofer_producto o0_
INNER JOIN inv_producto i1_ ON (i1_.id = o0_.producto_id)
INNER JOIN ofer_oobra o2_ ON (o2_.id = o0_.oo_id)
INNER JOIN ofer_oferta o3_ ON (o2_.ofert_id = o3_.id)
WHERE o3_.id = 2
GROUP BY o0_.price, i1_.numero, i1_.nombre, i1_.um, i1_.id) UNION (
SELECT SUM(f0_.cant) AS sclr_0, f0_.price AS price_1, i1_.numero AS numero_2, i1_.nombre AS nombre_3, i1_.um AS um_4, i1_.id AS id_5
FROM fab_mate f0_
INNER JOIN inv_producto i1_ ON (i1_.id = f0_.producto_id)
INNER JOIN fab_produc f2_ ON (f2_.id = f0_.fab_id)
INNER JOIN ofer_fabricacion o3_ ON (f2_.id = o3_.fab_id)
INNER JOIN ofer_oobra o4_ ON (o4_.id = o3_.oo_id)
INNER JOIN ofer_oferta o5_ ON (o4_.ofert_id = o5_.id)
WHERE o5_.id = 2
GROUP BY f2_.code, f2_.name, f0_.price, i1_.numero, i1_.nombre, i1_.um)) AS tt
    
asked by Aslee Guirola 26.10.2016 в 18:11
source

2 answers

1

UNION is not currently supported by DQL.

However, you can use native SQL and resultset mapping, as indicated here: link

    
answered by 27.10.2016 в 11:05
0

As the comrade Muriano comments, in DQL you do not have support for UNION .

Note that Doctrine is composed of several subprojects:

  • Database abstraction layer, or DBAL .
  • Relational object mapping layer, u ORM .

It has more subprojects and components, but for what we are dealing with right now, these are the ones that interest us.

Within the ORM, you have several methods for querying the database:

Likewise, and thanks to the Hydration mechanisms, you can continue working with exactly the same objects; Here you can see it:

link

link

I recommend that you review the Doctrine documentation, which is not treated too much in the official documentation of Symfony (with good judgment, since it is a different project, and you do not have to use Doctrine as an ORM); it's a little bit cumbersome, but it will help you a lot to understand exactly what you're doing when you use Doctrine, when you use the QueryBuilder API, when you use DQL, etc .; besides that you see that the supposed bottlenecks of speed that an ORM can originate, can be solved perfectly, knowing how.

I hope it will be helpful, greetings.

    
answered by 27.10.2016 в 12:40