symfony2 按属性对对象集合进行排序属性、对象

2023-09-07 00:19:45 作者:烟敛寒林

我有这个实体:

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;

...