-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: support for joining subqueries #2305
Comments
@aimfeld what do you mean by DBAL does not support joining subqueries? Are you referring to DBAL's |
@deeky666 Yes, I was referring to the DBAL's QueryBuilder class. Thanks for considering this for |
You're welcome. Currently it looks like we most probably will have a lot more possibilities for |
After trying for some time, I found a solution to join subqueries with the DBAL QueryBuilder: http://stackoverflow.com/a/35656904/94289 |
Hi @aimfeld , I've tried your example but it's not working. Is it working for you? What Doctrine version do you use? Thanks. |
@samuel4x4 I have refactored this code since then to use doctrine DQL instead of DBAL. The DBAL solution worked for me using doctrine 2.5 DBAL |
@aimfeld Can you please show me how you did it, either with DQL or DBAL? Give me please an example that works. Thanks. |
I use this quick and dirty solution in the projects where it's needed: class QueryBuilder extends BaseQueryBuilder
{
public function importSubQuery(BaseQueryBuilder $subBuilder)
{
$params = $subBuilder->getParameters();
foreach ($params as $key => $value) {
$this->createPositionalParameter(
$value,
$subBuilder->getParameterType($key)
);
}
return $subBuilder->getSQL();
}
} It works when:
It works only with positional parameters, but named parameters are not portable anyways. @deeky666 do you have any design requirements for this kind of feature? |
This is a big nasty can of worms. |
DBAL internally changes named params to positional ones so there's no reason to use positional ones except convenience. In fact, I'd say in Ideally the subquery would only be parsed when the main query is executed. This would let you add a subquery and alter its contents after the fact. When a When the query is compiled in This way both the parent and subquery can be changed after assignment and both can be individually executed. Additionally you can use the same subquery in multiple main queries. But as reading this has probably let on - it's no small tweak. (Just imagine recursing query detection!) In the meantime your best bet is to make your subquery parameters from the main query, and make your subquery all in one go: // Note that we create the parameter on the main query
$subquery->where('y = '.$query->createNamedParameter($value));
$query->innerJoin('main', $subquery->getSQL(), 'sub', 'sub.x = main.y'); |
Joining subqueries seems quite an important feature. However, as discussed here, DBAL does not support this currently. While DBAL looks great otherwise, for me this is a dealbreaker unfortunately. Is there any chance this could be implemented?
The text was updated successfully, but these errors were encountered: