I am using Doctrine for one-to-many mapping, such as a person (Person) and multiple children (Children). I hope that when using Doctrine to automatically obtain Children, the children can be sorted by age.
Excuse me: Which function should be overloaded to achieve this? persion->Which function does getChildren() call to perform SQL query?
It is difficult to find results for such a demand, so please give me some advice.
The solution is as follows:
Modification
Person
类的getChildren
MethodIf you don’t want to modify it
getChildren
方法,可以写一个新的方法getChildrenOrderedByAge
, the same principle applies.Summary:
Doctrine
的一对多或者多对多关系中,Entity
中所谓多
的属性是DoctrineCommonCollectionsCollection
接口的某一实现的实例,默认情况下是DoctrineCommonCollectionsArrayCollection
,上述解决方案中用到的就是这一接口的Filtering API
(筛选接口),上述情况下,筛选的条件会最终转化到SQL
layer processing to achieve performance optimization.Finally, the relevant official document link is as follows: Filtering Collections
It’s okay to use @vinzao’s method, but Doctirne provides the
OrderBy
method:Person:
Children: