简体   繁体   English

如何按“学说”中的条件排序结果?

[英]How can I order results by conditions in Doctrine?

How can I order results with the Doctrine Query Builder so that the results matching the first condition go first, then the results of the second condition (and all results should be matching the third condition)? 如何使用“ Doctrine查询生成器”对结果进行排序,以使匹配第一个条件的结果排在第一位,然后是第二个条件的结果(所有结果都应与第三个条件匹配)?

Here is an example of the query: 这是查询的示例:

$QB->select('k', 'k.word')
        ->from($repository, 'k')
        ->where('k.word LIKE :strict')
        ->orWhere('k.word LIKE '.':loose')       
        ->andWhere('k.status=1')
        ->setMaxResults(5)
        ->setParameter('strict', $query.'%')
        ->setParameter('loose', '%'.$query.'%');

I tried to play with orderBy() but it didn't work out - don't know what syntax could be.... 我尝试使用orderBy()但没有成功-不知道语法是什么。

Upd. UPD。 Example list of data and result: Let's say I have a list of data: 数据和结果列表示例:假设我有一个数据列表:

  • Word: Car , Status: 1 字: 汽车 ,状态: 1
  • Word: Carwash , Status: 1 词: 洗车 ,状态: 1
  • Word: Electric car , Status: 1 词: 电动车 ,状态: 1
  • Word: Cartography , Status: 1 字: 制图 ,状态: 1
  • Word: Cabriolet , Status: 0 字: 敞蓬车 ,状态: 0

And the search query is "ca" . 搜索查询是“ ca”

I want the query to order and show these results first: 我希望查询进行排序并首先显示这些结果:

  • Word: Car , Status: 1 字: 汽车 ,状态: 1
  • Word: Carwash , Status: 1 词: 洗车 ,状态: 1
  • Word: Cartography , Status: 1 字: 制图 ,状态: 1

Then this result: 然后这个结果:

  • Word: Electric car , Status: 1 词: 电动车 ,状态: 1

And filter out this one, because the status is 0 : 并过滤掉这个,因为状态为0

  • Word: Cabriolet , Status: 0 字: 敞蓬车 ,状态: 0

Have you tried this ? 你尝试过这个吗?

$QB->select('k', 'k.word')
        ->from($repository, 'k')
        ->where('k.word LIKE :strict')
        ->orWhere('k.word LIKE '.':loose')       
        ->andWhere('k.status=1')
        ->setMaxResults(5)
        ->setParameter('strict', $query.'%')
        ->setParameter('loose', '%'.$query.'%')
        ->orderBy('k.word', 'ASC');

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

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