如何在 symfony2 中对实体的 arrayCollection 进行排序 [英] how to sort an entity's arrayCollection in symfony2

查看:25
本文介绍了如何在 symfony2 中对实体的 arrayCollection 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这个属性的实体容器"

I have an entity "container" with this property

/**
 * @ORMOneToMany(targetEntity="BizTVContentManagementBundleEntityContent", mappedBy="container")
 */
private $content;

该属性是一个数组集合...

the property is an array collection...

public function __construct() {
    $this->content = new DoctrineCommonCollectionsArrayCollection();
}

...用这两种标准方法

...with these two standard methods

/**
 * Add content
 *
 * @param BizTVContentManagementBundleEntityContent $content
 */
public function addContent(BizTVContentManagementBundleEntityContent $content)
{
    $this->content[] = $content;
}

/**
 * Get content
 *
 * @return DoctrineCommonCollectionsCollection 
 */
public function getContent()
{
    return $this->content;
}

现在我的问题是,是否有一种流畅的方法可以在其中构建排序功能,也许是在 getContent() 调用中?我不是 php wiz,当然也没有在 symfony2 方面经验丰富,但我边走边学.

Now my question is, is there a smooth way to build a sorting feature into this, perhaps on the getContent() call? I am no php wiz and certainly not seasoned in symfony2 but I learn as I go.

内容实体本身有一个像这样的排序 INT,我想对其进行排序:

The content entity itself has a sorting INT like this that I want to sort it on:

/**
 * @var integer $sortOrder
 *
 * @ORMColumn(name="sort_order", type="integer")
 */
private $sortOrder; 

推荐答案

你应该可以使用 @ORMOrderBy 语句,允许您指定列以对集合进行排序:

You should be able to use the @ORMOrderBy statement which allows you to specify columns to order collections on:

/**
 * @ORMOneToMany(targetEntity="BizTVContentManagementBundleEntityContent", mappedBy="container")
 * @ORMOrderBy({"sort_order" = "ASC"})
 */
private $content;

事实上,这可能与 How to OrderBy on OneToMany/ManyToOne

检查实施建议,您似乎必须使用集合查询获取表,以便 @ORMOrderBy 注释起作用:http://www.krueckeberg.org/notes/d2.html

Checking for implementation advice it appears that you must fetch the tables with a join query to the collection in order for the @ORMOrderBy annotation to work: http://www.krueckeberg.org/notes/d2.html

这意味着您必须在存储库中编写一个方法来返回包含内容表的容器.

This means that you must write a method in the repository to return the container with the contents table joined.

这篇关于如何在 symfony2 中对实体的 arrayCollection 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆