Skip to content

Commit c45f900

Browse files
committed
Merge pull request #156 from vincent-chapron/fixbug/add-order-by-query
Fixbug/add order by query
2 parents 19b2119 + 3ca30d2 commit c45f900

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Bundle/QueryBundle/Entity/Traits/QueryTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ trait QueryTrait
1414
*/
1515
protected $query;
1616

17+
/**
18+
* @var string
19+
*
20+
* @ORM\Column(name="orderBy", type="text", nullable=true)
21+
*/
22+
protected $orderBy;
23+
1724
/**
1825
* Auto list mode: businessentity type
1926
* @var string
@@ -42,6 +49,26 @@ public function setQuery($query)
4249
$this->query = $query;
4350
}
4451

52+
/**
53+
* Get orderBy
54+
*
55+
* @return string
56+
*/
57+
public function getOrderBy()
58+
{
59+
return $this->orderBy;
60+
}
61+
62+
/**
63+
* Set orderBy
64+
*
65+
* @param string $orderBy
66+
*/
67+
public function setOrderBy($orderBy)
68+
{
69+
$this->orderBy = $orderBy;
70+
}
71+
4572
/**
4673
* Get businessEntity
4774
*

Bundle/QueryBundle/Helper/QueryHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function buildWithSubQuery($containerEntity, QueryBuilder $itemsQueryBuil
125125

126126
//get the query of the container entity
127127
$query = $containerEntity->getQuery();
128+
$orderBy = json_decode($containerEntity->getOrderBy(), true);
128129
if ($query !== '' && $query !== null) {
129130

130131
$subQuery = $this->entityManager->createQueryBuilder()
@@ -133,6 +134,11 @@ public function buildWithSubQuery($containerEntity, QueryBuilder $itemsQueryBuil
133134

134135
$itemsQueryBuilder
135136
->andWhere('main_item.id IN ('.$subQuery->getQuery()->getDql().' '.$query.')');
137+
if ($orderBy) {
138+
foreach ($orderBy as $addOrderBy) {
139+
$itemsQueryBuilder->addOrderBy('main_item.' . $addOrderBy['by'], $addOrderBy['order']);
140+
}
141+
}
136142
}
137143
$currentView = $this->currentView;
138144

0 commit comments

Comments
 (0)