Doctrine 2.1: How to orderBy aggregate field in a collection?
Asked Answered
C

2

6

I have an entity with a ArrayCollection field. In the annotations I can write

@ORM\OrderBy({"somefield" = "DESC"})

and the collection I get from that entity will be automatically ordered.

My question is if it is possible to order by aggregate fields?

My current issue: I have 2 fields in the collection entity: visited and shown, I would like to sort the collection by a rating index, which is: visited / shown. I tried writting it in the annotations, but it says it should be a valid field. I know how to do this with DQL, but I don't know how to recreate it in the Entity's declaration with annotations.

Thanks in advance!

Chauffer answered 6/3, 2012 at 22:0 Comment(0)
W
1

You need math operation in ORDER BY statement - in SQL it would be ORDER BY visited/shown DESC. I think it's not possible to do within annotations.

Wendolynwendt answered 1/4, 2014 at 21:23 Comment(0)
P
3

Not quite sure I do understand your problem. But what does

@ORM/OrderBy({"visited" = "DESC", "shown" = "DESC"})

give as result?

From http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-orderby :

The DQL Snippet in OrderBy is only allowed to consist of unqualified, unquoted field names and of an optional ASC/DESC positional statement. Multiple Fields are separated by a comma (,). The referenced field names have to exist on the targetEntity class of the @ManyToMany or @OneToMany annotation.

Prolonge answered 1/6, 2012 at 11:18 Comment(2)
No he need math operation in ORDER BY statement - in SQL it would be ORDER BY visited/shown DESC. I think it's not possible to do within annotationsAjmer
@MaciejPyszyński You are correct, it's not possible at this point, can you post it as an answer, so I can accept it?Chauffer
W
1

You need math operation in ORDER BY statement - in SQL it would be ORDER BY visited/shown DESC. I think it's not possible to do within annotations.

Wendolynwendt answered 1/4, 2014 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.