mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Support UNION in QueryBuilder #5014
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @olegkrivtsov on GitHub (Feb 8, 2016).
Originally assigned to: @Ocramius on GitHub.
Hi,
Why the QueryBuilder doesn't allow to create UNION queries? In my concrete situation, I need a UNION query instead of JOIN because of performance reason.
@Ocramius commented on GitHub (Feb 8, 2016):
DQL doesn't support
UNION, asUNIONallows aggregating non-homogeneous datasets, which doesn't really work in a strictly typed DSL (in this case DQL).@Ocramius commented on GitHub (Feb 8, 2016):
@olegkrivtsov this is one of those cases where you'd just really use raw SQL instead of a high level abstraction. Having an
UNIONthat works in this context (and can't be efficiently reproduced with multiple queries) is really rare@olegkrivtsov commented on GitHub (Feb 8, 2016):
Hi @Ocramius I can replace the UNION with several simple DQL queries, but I'll have to merge and sort the results somehow. Does Doctrine have a way to merge/order the results of several DQL queries?
@Ocramius commented on GitHub (Feb 8, 2016):
@olegkrivtsov good point. No, there is no such thing in DQL itself: that would be an array merge operation, IMO.
Assuming you have a query (pseudo-code, silly, but serves the purpose of the example) like following:
You can probably re-write this query to be a single SQL (SQL, not DQL!) query that extracts just the identifiers:
Then select the entities by identifier (careful: works only up to 1000 elements on some PDO wrappers):
Then loop over
$firstQueryResultand fetch the data:This is just an example, of course, but I hope it helps.
@Raffaello commented on GitHub (Sep 8, 2016):
@Ocramius Would it be possible at least to return the parameter key names from a
getSQL()method?because they are all replaced by
?and in the case from a query builder retrieve the SQL, make the union, with another SQL and then run the native query, it is so quite annoying and error prone remap every single parameter without the key.Even more how to deal with
HIDDENDQL column when retrieving the SQL? if there is a way..thanks