简体   繁体   English

使用Doctrine orderBy的Zendframework2根本不是“排序”

[英]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. 这两列都存在,也没有得到PHP / Doctrine / Mysql错误。 Is this the right aproach? 这是正确的方法吗? I also tried using orderBy Annotations in my Entity with no success either: 我也尝试在我的实体中使用orderBy注释,但也没有成功:

/**
* @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. 我也确实读过有关QueryBuilder和DQL的信息,但这似乎有点难以获得orderBy设置。

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. 现在,如果Cbox \\ Entity \\ Category\\ Cbox \\ Entity \\ Category是正确的实体,则它必须起作用。 Here is another approach you can do same thing without setParameter() but i will not recommend it. 这是另一种无需setParameter()即可执行相同操作的方法,但我不会推荐它。

  $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 如果您想了解如何从$ qb中提取结果,我建议您点击此链接http://docs.doctrine-project.org/en/latest/reference/query-builder.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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