symfony doctrine order by

时间:2015-01-03 22:23:41

标签: php symfony doctrine-orm doctrine

我正在尝试使用教义并采用有序的值,但我不能。

我试试:

$articlesB = $this
                ->getDoctrine()
                ->getManager()
                ->getRepository('theBundle:Article')
                ->findAll(array('date' => 'ASC'));

您是否知道如何按日期排序此值?列被命名为date并占用所有日期。我想要这个订货人。

由于 最好的问候

2 个答案:

答案 0 :(得分:3)

使用findBy代替findAll,第一个参数(选择条件)使用空数组,第二个参数使用排序数组。

$articlesB = $this
            ->getDoctrine()
            ->getManager()
            ->getRepository('theBundle:Article')
            ->findBy(array(),array('date' => 'ASC'));

在这种情况下,我查看了实际的源代码。你会认为findAll()可以工作但不行。它从未通过排序标准。

答案 1 :(得分:0)

您需要创建一个ArticleRepostory并在其中:

public function getOrderedArticles()
{
    $return $this->getEntityManager()
            ->createQuery(
            "SELECT a FROM theBundle:Article a "
            . "ORDER BY a.date ASC"
    );
}

以便您的控制器可以

$articlesB = $this
                ->getDoctrine()
                ->getManager()
                ->getRepository('theBundle:Article')
                ->getOrderedArticles();