Search code examples
phpsymfonydoctrine-ormsql-order-byarraycollection

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


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!


Solution

  • 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.