how to sort an entity's arrayCollection in symfony2

后端 未结 4 1719
借酒劲吻你
借酒劲吻你 2021-02-01 15:33

I have an entity \"container\" with this property

/**
 * @ORM\\OneToMany(targetEntity=\"BizTV\\ContentManagementBundle\\Entity\\Content\", mappedBy=\"container\"         


        
4条回答
  •  [愿得一人]
    2021-02-01 16:18

    You can also sort ArrayCollection by Criteria property orderBy like so:

    getCollection();
    
                // convert normal array to array collection object
                if(\is_array(collection)) {
                    $collection = new ArrayCollection(collection);
                }
    
                // order collection items by position property
                $orderBy = (Criteria::create())->orderBy([
                    'position' => Criteria::ASC,
                ]);
    
                // return sorter SomeCollectionItem array
                return $collection->matching($orderBy)->toArray();
            }
        }
    ?>
    

提交回复
热议问题