Doctrine 2.1: How to Arrange Aggregate Field in a Collection?

I have an object with an ArrayCollection field. In annotations I can write

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

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

My question is, can I sort by aggregate fields?

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

Thanks in advance!

+6
source share
2 answers

You need a mathematical operation in the ORDER BY statement - in SQL it will be ORDER BY visited / shown by DESC. I think this cannot be done in annotations.

+1
source

Not quite sure I understand your problem. But what does

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

as a 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 for unqualified, unquoted field names, and the optional ASC / DESC positional operator. Multiple fields are separated by a comma (,). Reference field names must exist in the targetEntity class of the @ManyToMany or @OneToMany annotation.

+3
source

Source: https://habr.com/ru/post/910122/


All Articles