教条2.1:如何在集合中使用orderBy聚合字段?

时间:2021-09-17 22:49:34

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

我有一个具有ArrayCollection字段的实体。在注解中我可以写

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

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

我当前的问题:我在集合实体中有两个字段:已访问和显示,我想用一个评级索引对集合进行排序,即:已访问/显示。我试着在注释中写出来,但是它说它应该是一个有效的字段。我知道如何使用DQL实现这一点,但我不知道如何在实体的声明中使用注释重新创建它。

Thanks in advance!

提前谢谢!

2 个解决方案

#1


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.

你需要按语句顺序进行数学运算——在SQL中,它是按访问/显示DESC的顺序进行的。

#2


3  

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

我不太清楚你的问题。但是

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

@ORM / OrderBy({“访问”=“DESC”,“显示”=“DESC”})

give as result?

给结果吗?

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

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

OrderBy中的DQL代码片段只允许包含不合格的、未引用的字段名和可选的ASC/DESC位置语句。多个字段用逗号(,)分隔。引用的字段名必须存在于@ManyToMany或@OneToMany注释的targetEntity类上。

#1


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.

你需要按语句顺序进行数学运算——在SQL中,它是按访问/显示DESC的顺序进行的。

#2


3  

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

我不太清楚你的问题。但是

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

@ORM / OrderBy({“访问”=“DESC”,“显示”=“DESC”})

give as result?

给结果吗?

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

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

OrderBy中的DQL代码片段只允许包含不合格的、未引用的字段名和可选的ASC/DESC位置语句。多个字段用逗号(,)分隔。引用的字段名必须存在于@ManyToMany或@OneToMany注释的targetEntity类上。