symfony2按属性排序对象的集合 [英] symfony2 sort collection of objects by a property
本文介绍了symfony2按属性排序对象的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个实体:
class Categoria {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/** @ORM\Column(type="string", length=100) */
protected $nom;
/** @ORM\Column(type="string", length=100) */
protected $slug;
/** @ORM\Column(type="decimal", precision=3, scale=0) */
protected $ordre;
/** @ORM\Column(type="boolean", nullable=true) */
protected $actiu=FALSE;
/** @ORM\Column(type="decimal", precision=4, scale=0, nullable=true) */
protected $enllaç=null;
/** @ORM\OneToMany(targetEntity="LoPati\MenuBundle\Entity\subCategoria", mappedBy="categoria", cascade={"persist", "remove"} )*/
protected $subCategoria;
public function __construct()
{
$this->subCategoria = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add subCategoria
*
* @param LoPati\MenuBundle\Entity\subCategoria $subCategoria
*/
public function addsubCategoria(\LoPati\MenuBundle\Entity\subCategoria $subCategoria)
{
$this->subCategoria[] = $subCategoria;
}
/**
* Get subCategoria
*
* @return Doctrine\Common\Collections\Collection
*/
public function getSubCategoria()
{
return $this->subCategoria;
}
和
class SubCategoria {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/** @ORM\Column(type="string", length=100) */
protected $nom;
/** @ORM\Column(type="string", length=100) */
protected $slug;
/** @ORM\Column(type="decimal", precision=3, scale=0) */
protected $ordre;
/** @ORM\Column(type="boolean", nullable=true) */
protected $actiu=FALSE;
/** @ORM\Column(type="boolean", nullable=true) */
protected $llista=FALSE;
/** @ORM\Column(type="decimal", precision=4, scale=0, nullable=true) */
protected $enllaç=null;
/** @ORM\ManyToOne(targetEntity="Categoria", inversedBy="subCategoria") */
protected $categoria;
在Categoria实体中,我想按 $排序子类别对象的集合ordre
。
In Categoria entity I would like sort the collection of subcategoria Objects sort by $ordre
.
我该怎么做?
谢谢
问候
推荐答案
使用此注释:
/**
* @ORM\OneToMany(targetEntity="LoPati\MenuBundle\Entity\subCategoria", mappedBy="categoria", cascade={"persist", "remove"} )
* @ORM\OrderBy({"order" = "DESC", "id" = "DESC"})
*/
protected $subCategoria;
...
这篇关于symfony2按属性排序对象的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文