symfony2按属性排序对象集合

Mar*_*rez 7 symfony doctrine-orm twig

我有这样的实体:

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

在Categoria实体中,我想排序子类别对象排序依据的集合$ordre.

我怎么能这样做?有可能在Twig templeate或实体的definitios吗?

谢谢

问候

gui*_*ier 34

使用此注释:

/** 
 * @ORM\OneToMany(targetEntity="LoPati\MenuBundle\Entity\subCategoria", mappedBy="categoria", cascade={"persist", "remove"} )
 * @ORM\OrderBy({"order" = "DESC", "id" = "DESC"})
*/
protected $subCategoria;
Run Code Online (Sandbox Code Playgroud)

...

  • 完善!谢谢! (3认同)