何在symfony2中对实体的arrayCollection进行

何在symfony2中对实体的arrayCollection进行

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

问题描述

我有一个具有此属性的实体容器"

I have an entity "container" with this property

/**
 * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
 */
private $content;

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

the property is an array collection...

public function __construct() {
    $this->content = new \Doctrine\Common\Collections\ArrayCollection();
}

...使用这两种标准方法

...with these two standard methods

/**
 * Add content
 *
 * @param BizTV\ContentManagementBundle\Entity\Content $content
 */
public function addContent(\BizTV\ContentManagementBundle\Entity\Content $content)
{
    $this->content[] = $content;
}

/**
 * Get content
 *
 * @return Doctrine\Common\Collections\Collection
 */
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
 *
 * @ORM\Column(name="sort_order", type="integer")
 */
private $sortOrder;

推荐答案

您应该可以使用 @ ORM \ OrderBy 语句,该语句可用于指定要对以下项进行订购的列:

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

/**
 * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
 * @ORM\OrderBy({"sort_order" = "ASC"})
 */
private $content;

实际上,这可能是如何在OneToMany/ManyToOne上订购的重复方式

在检查实施建议后,您似乎必须使用对集合的联接查询来获取表,才能使@ORM \ OrderBy批注起作用: 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 @ORM\OrderBy 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进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:15