-
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
Hidden fields #151
Comments
Example solution Create ValueObject for hidden field and use it in conditions: $field = new HiddenField('SUM(%s.viewed)', 'views', $dqlAlias); // $dqlAlias nullable
$spec = Spec::andX(
Spec::addHiddenField($field),
Spec::orderBy($field, 'DESC')
); In class OrderBy implements QueryModifier
{
// ...
public function modify(QueryBuilder $qb, $dqlAlias)
{
if ($this->dqlAlias !== null) {
$dqlAlias = $this->dqlAlias;
}
if ($this->field instanceof HiddenField) {
$field = $this->field->getAlias();
} else {
$field = sprintf('%s.%s', $dqlAlias, $this->field);
}
$qb->addOrderBy($field, $this->order);
}
} |
Now we ca use hidden fields for order result: // DQL: SELECT e, e.posts_count + e.likes_count HIDDEN score FROM User e ORDER BY score
$spec = Spec::andX(
Spec::addSelect(
Spec::selectHiddenAs(
Spec::add(Spec::field('posts_count'), Spec::field('likes_count')),
'score'
)
),
Spec::orderBy(
Spec::alias('score')
)
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some queries require complex conditions that can not be realized using your library.
Sometimes we need to add to the query a hidden field that will only be used for conditions.
Example from Doctrine docs:
Example implementation in my project as specification:
The text was updated successfully, but these errors were encountered: