symfony2 按属性对对象集合进行排序 [英] symfony2 sort collection of objects by a property

查看:16
本文介绍了symfony2 按属性对对象集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个实体:

class Categoria {

    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue
     */

    protected $id;

    /** @ORMColumn(type="string", length=100) */
    protected $nom;

    /** @ORMColumn(type="string", length=100) */
    protected $slug;

    /** @ORMColumn(type="decimal", precision=3, scale=0) */
    protected $ordre;

    /** @ORMColumn(type="boolean", nullable=true) */
    protected $actiu=FALSE;

    /** @ORMColumn(type="decimal", precision=4, scale=0, nullable=true) */
    protected $enllaç=null;

    /** @ORMOneToMany(targetEntity="LoPatiMenuBundleEntitysubCategoria", mappedBy="categoria", cascade={"persist", "remove"} )*/
     protected $subCategoria; 
public function __construct()
{
    $this->subCategoria = new DoctrineCommonCollectionsArrayCollection();

}

/**
 * Add subCategoria
 *
 * @param LoPatiMenuBundleEntitysubCategoria $subCategoria
 */
public function addsubCategoria(LoPatiMenuBundleEntitysubCategoria $subCategoria)
{
    $this->subCategoria[] = $subCategoria;
}

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

class SubCategoria {

/**
 * @ORMId
 * @ORMColumn(type="integer")
 * @ORMGeneratedValue
 */

protected $id;

/** @ORMColumn(type="string", length=100) */
protected $nom;

/** @ORMColumn(type="string", length=100) */
protected $slug;

/** @ORMColumn(type="decimal", precision=3, scale=0) */
protected $ordre;

/** @ORMColumn(type="boolean", nullable=true) */
protected $actiu=FALSE; 

/** @ORMColumn(type="boolean", nullable=true) */
protected $llista=FALSE;

/** @ORMColumn(type="decimal", precision=4, scale=0, nullable=true) */
protected $enllaç=null;

/** @ORMManyToOne(targetEntity="Categoria", inversedBy="subCategoria") */

protected $categoria;

在类别实体中,我想按 $ordre 对子类别对象的集合进行排序.

In Categoria entity I would like sort the collection of subcategoria Objects sort by $ordre.

我该怎么做?是否可以在 Twig 模板或实体的定义中进行?

How I can do it ? Is possible do it in Twig templeate or in the definitios of Entity?

谢谢

问候

推荐答案

使用这个注解:

/** 
 * @ORMOneToMany(targetEntity="LoPatiMenuBundleEntitysubCategoria", mappedBy="categoria", cascade={"persist", "remove"} )
 * @ORMOrderBy({"order" = "DESC", "id" = "DESC"})
*/
protected $subCategoria;

...

这篇关于symfony2 按属性对对象集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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