1

I really am at a point where I am out of Ideas. I just want to order categories with Doctrine. After a litle doctrine documentation reading I uses following in my Controller:

$categories = $em->getRepository('\Cbox\Entity\Category')->findBy(array('title' =>'news'), array('createdTime' => 'DESC'));

Both Column's exist nor do I get a PHP/Doctrine/Mysql Error. Is this the right aproach? I also tried using orderBy Annotations in my Entity with no success either:

/**
* @ORM\OneToMany(targetEntity="Cbox\Entity\Category", mappedBy="box")
* @ORM\OrderBy({"createdTime" = "DESC"})
*/
protected $category;

I also did read about the QueryBuilder and the DQL but this just seems to be a litle bit of an overkill to get the orderBy set.

Any help would be much appreciated. I also do hope thats enough code for you guys to get the picture. But I rather have the "problematic" parts shown here then tons of code.

4

1 回答 1

1

尝试这个

 $qb = $em->createQueryBuilder();
 $qb->select('u')
 ->from('Cbox\Entity\Category u')
 ->where('u.title = :title')
 ->orderBy('u.createdTime', 'DESC')
 ->setParameter('title', news);

现在,如果Cbox\Entity\Category\Cbox\Entity\Category是正确的实体,这必须有效。这是另一种方法,你可以在没有setParameter()的情况下做同样的事情,但我不推荐它。

  $qb->select('u')
 ->from('Cbox\Entity\Category u')
 ->where('u.title = news')
 ->orderBy('u.createdTime', 'DESC');

此外,如果您想查看如何从$qb中获取结果,我建议您点击此链接http://docs.doctrine-project.org/en/latest/reference/query-builder.html

于 2013-07-21T02:14:53.837 回答