-
Notifications
You must be signed in to change notification settings - Fork 41
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
Using subqueries #299
Labels
Comments
My workaround is to implement custom Operand something like:
And than use:
|
Perhaps you shouldn't explicitly specify the DQL alias? final class InSubquery implements Filter
{
private string $field;
private QueryBuilder $subqueryBuilder;
private ?string $dqlAlias;
public function __construct(string $field, QueryBuilder $subqueryBuilder, ?string $dqlAlias = null)
{
$this->field = $field;
$this->subqueryBuilder = $subqueryBuilder;
$this->dqlAlias = $dqlAlias;
}
public function getFilter(QueryBuilder $qb, string $dqlAlias): string
{
if (null !== $this->dqlAlias) {
$dqlAlias = $this->dqlAlias;
}
foreach ($this->subqueryBuilder->getParameters() as $parameter) {
$qb->setParameter($parameter->getName(), $parameter->getValue(), $parameter->getType());
}
return (string) $qb->expr()->in(
\sprintf('%s.%s', $dqlAlias, $this->field),
$this->subqueryBuilder->getDQL(),
);
}
} Usage $subqueryBuilder = $repo->getQueryBuilder(Spec::orX(
Spec::eq('name', 'a'),
Spec::like('name', 'a', Like::CONTAINS),
));
$qb = $repo->getQueryBuilder(new InSubquery('id', $subqueryBuilder)); |
Let me remind you that you can turn off context resolving for a specific query. DQLContextResolver::disableDeadJoinsProtection();
$q = $repo->getQueryBuilder(
Spec::orX(
Spec::like('name', 'a', Like::CONTAINS, 's'),
Spec::eq('name', 'a', 's')
),
's'
)->getQuery();
$q = $repo->getQuery(Spec::andX(
new InSubquery('id', $q, 's')
));
DQLContextResolver::enableDeadJoinsProtection(); Or you can disable auto joining DQLContextResolver::disableAutoJoining();
// your code here
DQLContextResolver::enableAutoJoining(); |
Thanks Peter, I will try that out. |
Turning off the context resolving works for me. Thanks a lot! Good work! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used to use something like:
but it does not work with 2.0... (using happyr/doctrine-specification 2.x-dev dc6c2b5 )
The query tries to join
s
... and produces invalid DQL.I tried to understand the new context from #273 but I do not know how to fix my code.... Can you please provide some hints?
Thanks
The text was updated successfully, but these errors were encountered: