2012-08-11 15 views
6

que tienen estas entidades:Symfony2 colección tipo de objetos por una propiedad

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; 
} 

Y

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; 

En Categoria entidad quisiera ordenar la colección de subcategoria Objetos ordenar por $ordre.

¿Cómo puedo hacerlo? ¿Es posible hacerlo en Twath Templeate o en las Definiciones de Entity?

Gracias

Saludos

Respuesta

32

Utilice esta anotación:

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

...

+3

perfecto! ¡Gracias! –

+0

No funciona para mí, por alguna razón ... –

Cuestiones relacionadas