我有2个实体Task和TaskPriority,关系有效。
我希望显示所有任务,但按照优先顺序排序:
紧急
- 任务#1
- 任务#3
- 任务#8
待办事项
- 任务#2
- 任务#4
- 任务#7
我试过了,我知道它不起作用,但它显示了我想要做的事情:
{% for priority in tasks.priority %}
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="glyphicon glyphicon-exclamation-sign"></i>{{ priority.title }}
</div>
<ul class="list-group">
<li class="list-group-item">
{{ task.title }}
</li>
</ul>
</div>
</div>
{% endfor %}
这样做的正确方法是什么?
修改
我的TaskRepository中已有一些方法:
class TaskRepository extends EntityRepository
{
public function grabAll()
{
$queryBuilder = $this->createQueryBuilder('t')
->leftJoin('t.category', 'c')->addSelect('c')
->leftJoin('t.user', 'u')->addSelect('u')
->leftJoin('t.project', 'p')->addSelect('p')
->orderBy('t.id', 'desc');
return $queryBuilder->getQuery()->getResult();
}
public function findFilteredItems($users, $categories, $projects, $search)
{
$queryBuilder = $this->createQueryBuilder('t')
->leftJoin('t.user', 'u')->addSelect('u')
->leftJoin('t.category', 'c')->addSelect('c')
->leftJoin('t.project', 'p')->addSelect('p')
->orderBy('t.id', 'desc');
if($users) {
foreach($users as $id) {
$queryBuilder->orHaving('u.id = :id')->setParameter('id', $id);
}
}
if($categories) {
foreach($categories as $id) {
$queryBuilder->orHaving('c.id = :id')->setParameter('id', $id);
}
}
if($projects) {
foreach($projects as $id) {
$queryBuilder->orHaving('p.id = :id')->setParameter('id', $id);
}
}
if($search) {
$queryBuilder->orWhere('t.title LIKE :search')->setParameter('search', $search);
}
return $queryBuilder->getQuery()->getResult();
}
}