Skip to content
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

OrderBy (and others) not working with aliased fields #127

Closed
cdomigan opened this issue Apr 27, 2016 · 5 comments
Closed

OrderBy (and others) not working with aliased fields #127

cdomigan opened this issue Apr 27, 2016 · 5 comments

Comments

@cdomigan
Copy link

The OrderBy Query Modifier (and others) does not support using aliased fields.

$qb->addOrderBy(sprintf('%s.%s', $dqlAlias, $this->field), $this->order);

This line enforces a table.field regardless of the dqlAlias passed in. Whereas I will often want to order on an aliased field (with no dot).

@KyHikari
Copy link

Same problem for the comparisons on aliased fields.

@Nyholm
Copy link
Member

Nyholm commented Jun 28, 2016

Let's see if we can support aliased fields. Can you give an example query using specs that you would like to work?

@mitrae
Copy link

mitrae commented Apr 25, 2019

@Nyholm the simplest way would to not add alias if it is empty
'' == $dqlAlias ? $this->field : sprintf('%s.%s', $dqlAlias, $this->field)

in this case i can add some complex field (with SUM, CASE, etc) into select and use it later in specs

@peter-gribanov
Copy link
Member

@mitrae this problem will be solved #169 in the next major release.

@peter-gribanov
Copy link
Member

Now you can order by aliases:

// 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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants