使用OrderBy Annotation问题对ArrayCollection进行排序

时间:2015-12-06 20:16:16

标签: php symfony doctrine-orm one-to-many arraycollection

我正在尝试从检索到的实体按特定顺序对ArrayCollection进行排序,但@ORM \ OrderBy不会触发。

Showcase.php:

/*...*/
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity **/
class Showcase
{
    /*...*/
    /** @ORM\OneToMany(targetEntity="ShowcaseGelato", mappedBy="showcase", fetch="EAGER")
     * @ORM\OrderBy({"position" = "ASC"}) **/
    private $gelatos;

    public function __construct()
    {
        $this->gelatos = new ArrayCollection();
    }
    /*...*/

ShowcaseGelato.php:

    use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
*/
class ShowcaseGelato
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Gelato", fetch="EAGER")
     */
    private $gelato;

    /**
     * @ORM\ManyToOne(targetEntity="Showcase", inversedBy="gelatos")
     * @ORM\JoinColumn(name="showcase_id", referencedColumnName="id")
     */
    private $showcase;

    /**
     * @ORM\Column(name="position", type="integer")
     */
    private $position;

ShowcaseController.php:

/*...*/
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $showcase = $em->getRepository('TodaysGelatoBundle:Showcase')->find(1);
        return $this->render(
            'TodaysGelatoBundle:Showcase:index.html.twig', array(
                'showcase' => $showcase,
            )
        );
    }

结果如下: enter image description here

我错过了什么?任何帮助都会被贬低。

数据库项目视图:enter image description here

0 个答案:

没有答案