简体   繁体   中英

Zendframework2 using Doctrine orderBy is not “ordering” at all

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.

Try this

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

Now if Cbox\\Entity\\Category or \\Cbox\\Entity\\Category is correct entity this must work. Here is another approach you can do same thing without setParameter() but i will not recommend it.

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

Further more if you want see how to fetch result out of $qb i suggest you follow this link http://docs.doctrine-project.org/en/latest/reference/query-builder.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM