Skip to content

Commit 5c268c7

Browse files
authored
Merge pull request #78 from SilverSnake83/master
fix(filterModifier): fix orderBy on relations
2 parents 23b7aed + 300b9eb commit 5c268c7

File tree

5 files changed

+39
-93
lines changed

5 files changed

+39
-93
lines changed

src/Service/QueryModifier/Easy/Modifier.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ public function methodExists($method)
8080
public function orderBy(string $dotProperty, $order = Criteria::ASC): ModelCriteria
8181
{
8282
$property = $this->before($dotProperty);
83-
$this->query = $this->query->orderBy($property, $order);
83+
$method = $this->buildMethodName(__FUNCTION__, $property);
84+
try{
85+
$this->query = $this->query->{$method}($order);
86+
} catch (\Throwable $e) {
87+
$this->failures[] = $method;
88+
}
8489
$this->after();
8590

8691
return $this->query;

src/Service/Request/QueryModifier/Modifier/Base/ModifierBase.php

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ public function __construct(ServerRequestInterface $request)
4141
$this->setModifierFromRequest($request);
4242
}
4343

44-
/**
45-
* Apply modifiers
46-
*
47-
* @param \Propel\Runtime\ActiveQuery\ModelCriteria $query
48-
* @param string $clause
49-
* @param array $modifier
50-
*
51-
* @return void
52-
*/
53-
abstract protected function applyModifier(ModelCriteria $query, $clause, array $modifier);
54-
5544
/**
5645
* Return the name of the modifier
5746
*
@@ -73,33 +62,7 @@ abstract protected function hasAllRequiredData(array $modifier);
7362
*
7463
* @param \Propel\Runtime\ActiveQuery\ModelCriteria $query
7564
*/
76-
public function apply(ModelCriteria $query)
77-
{
78-
if (!empty($this->modifiers)) {
79-
foreach ($this->modifiers as $modifier) {
80-
if ($this->hasAllRequiredData($modifier)) {
81-
$modifier['property'] = str_replace('/', '.', $modifier['property']);
82-
# Check if the filter is occurring on a related model
83-
if (strpos($modifier['property'], '.') !== false) {
84-
$propertyParts = explode('.', $modifier['property']);
85-
86-
# The last part is the related property we want to filter with, so remove it from the parts and store into a variable
87-
$propertyField = array_pop($propertyParts);
88-
# The new last part is the relation name
89-
$relationName = array_pop($propertyParts);
90-
91-
# Apply the modifier
92-
$this->applyModifier($query, $this->buildClause($propertyField, $relationName), $modifier);
93-
} else {
94-
# Apply the modifier
95-
$this->applyModifier($query,
96-
$this->buildClause($modifier['property'], $query->getModelShortName()),
97-
$modifier);
98-
}
99-
}
100-
}
101-
}
102-
}
65+
abstract public function apply(ModelCriteria $query);
10366

10467
/**
10568
* @param $property
@@ -181,21 +144,6 @@ public function setModifierFromRequest(ServerRequestInterface $request)
181144
$this->modifiers = $modifiers;
182145
}
183146

184-
/**
185-
* @param $property
186-
* @param null $modelOrRelationName
187-
*
188-
* @return string
189-
*/
190-
protected function buildClause($property, $modelOrRelationName)
191-
{
192-
$clause = ucfirst($property);
193-
if ($modelOrRelationName) {
194-
$clause = sprintf('%s.%s', ucfirst($modelOrRelationName), $clause);
195-
}
196-
197-
return $clause;
198-
}
199147

200148
/**
201149
* @param $property

src/Service/Request/QueryModifier/Modifier/FilterModifier.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ public function apply(ModelCriteria $query)
109109
return $modifierClass;
110110
}
111111

112-
/**
113-
* @inheritdoc
114-
*/
115-
protected function applyModifier(ModelCriteria $query, $clause, array $modifier)
116-
{
117-
}
118-
119112
/**
120113
* Has the modifier all required data to be applied?
121114
*

src/Service/Request/QueryModifier/Modifier/SortModifier.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Eukles\Service\Request\QueryModifier\Modifier;
1313

1414
use Eukles\Service\QueryModifier\Easy\Builder\Sort;
15+
use Eukles\Service\QueryModifier\Easy\Modifier;
1516
use Eukles\Service\Request\QueryModifier\Modifier\Base\ModifierBase;
1617
use Propel\Runtime\ActiveQuery\Criteria;
1718
use Propel\Runtime\ActiveQuery\Exception\UnknownColumnException;
@@ -65,17 +66,18 @@ public function getName()
6566
return self::NAME;
6667
}
6768

68-
/**
69-
* @inheritdoc
70-
*/
71-
protected function applyModifier(ModelCriteria $query, $clause, array $modifier)
69+
public function apply(ModelCriteria $query)
7270
{
73-
# Apply filter on the last related model query
74-
try {
75-
$query->orderBy($clause,
76-
(empty($modifier['direction']) ? self::DEFAULT_DIRECTION : $modifier['direction']));
77-
} catch (UnknownColumnException $e) {
78-
} catch (UnknownModelException $e) {
71+
$modifierClass = new Modifier($query);
72+
if (!empty($this->modifiers)) {
73+
foreach ($this->modifiers as $modifier) {
74+
if ($this->hasAllRequiredData($modifier)) {
75+
$modifierClass->orderBy(
76+
$modifier['property'],
77+
(empty($modifier['direction']) ? self::DEFAULT_DIRECTION : $modifier['direction'])
78+
);
79+
}
80+
}
7981
}
8082
}
8183

tests/Service/Request/QueryModifier/Modifier/SortModifierTest.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ModifierTestQuery;
1717
use PHPUnit\Framework\TestCase;
1818
use Propel\Generator\Util\QuickBuilder;
19+
use Propel\Runtime\Map\Exception\RelationNotFoundException;
1920
use Test\Eukles\Request;
2021

2122
/**
@@ -26,12 +27,17 @@
2627
class SortModifierTest extends TestCase
2728
{
2829

30+
/**
31+
* @var array to fill on the call of createSelectSql
32+
*/
33+
private $arr;
34+
2935
public function setUp()
3036
{
3137
if (!class_exists(ModifierTest::class)) {
32-
3338
$b = new QuickBuilder;
34-
$b->setSchema('
39+
$b->setSchema(
40+
'
3541
<database name="modifier_test_db">
3642
<table name="modifier_test">
3743
<column name="name" type="VARCHAR"/>
@@ -58,15 +64,15 @@ public function testApplyAsc()
5864
$m = new SortModifier(new Request(["sort" => json_encode(["property" => "name", "direction" => "asc"])]));
5965
$mc = new ModifierTestQuery();
6066
$m->apply($mc);
61-
$this->assertEquals('modifier_test.name ASC', $mc->getOrderByColumns()[0]);
67+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM ORDER BY modifier_test.name ASC");
6268
}
6369

6470
public function testApplyDesc()
6571
{
6672
$m = new SortModifier(new Request(["sort" => json_encode(["property" => "name", "direction" => "desc"])]));
6773
$mc = new ModifierTestQuery();
6874
$m->apply($mc);
69-
$this->assertEquals('modifier_test.name DESC', $mc->getOrderByColumns()[0]);
75+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM ORDER BY modifier_test.name DESC");
7076
}
7177

7278
public function testApplyMulti()
@@ -80,8 +86,10 @@ public function testApplyMulti()
8086
]));
8187
$mc = new ModifierTestQuery();
8288
$m->apply($mc);
83-
$this->assertEquals('modifier_test.name ASC', $mc->getOrderByColumns()[0]);
84-
$this->assertEquals('modifier_test.column2 ASC', $mc->getOrderByColumns()[1]);
89+
$this->assertEquals(
90+
$mc->createSelectSql($this->arr),
91+
"SELECT FROM ORDER BY modifier_test.name ASC,modifier_test.column2 ASC"
92+
);
8593
}
8694

8795
public function testSetModifierFromRequest()
@@ -116,7 +124,7 @@ public function testApplyWithoutDirectionIsDesc()
116124
$m = new SortModifier(new Request(["sort" => json_encode(["property" => "name"])]));
117125
$mc = new ModifierTestQuery();
118126
$m->apply($mc);
119-
$this->assertEquals('modifier_test.name DESC', $mc->getOrderByColumns()[0]);
127+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM ORDER BY modifier_test.name DESC");
120128
}
121129

122130
public function testGetName()
@@ -134,6 +142,7 @@ public function testInexistentRelation()
134142
]),
135143
]));
136144
$mc = new ModifierTestQuery();
145+
$this->expectException(RelationNotFoundException::class);
137146
$m->apply($mc);
138147
$this->assertEquals([], $mc->getOrderByColumns());
139148
}
@@ -149,20 +158,9 @@ public function testRelation()
149158
$mc = new ModifierTestQuery();
150159
$mc->joinWithRelationTest();
151160
$m->apply($mc);
152-
$this->assertEquals('relation_test.name ASC', $mc->getOrderByColumns()[0]);
153-
}
154-
155-
public function testRelationWithSlash()
156-
{
157-
$m = new SortModifier(new Request([
158-
"sort" => json_encode([
159-
"property" => "RelationTest/Name",
160-
"direction" => "asc",
161-
]),
162-
]));
163-
$mc = new ModifierTestQuery();
164-
$mc->joinWithRelationTest();
165-
$m->apply($mc);
166-
$this->assertEquals('relation_test.name ASC', $mc->getOrderByColumns()[0]);
161+
$this->assertEquals(
162+
$mc->createSelectSql($this->arr),
163+
"SELECT modifier_test.name, modifier_test.column2, modifier_test.date, modifier_test.relation_id, relation_test.name, relation_test.column2, relation_test.id FROM modifier_test INNER JOIN relation_test ON (modifier_test.relation_id=relation_test.id) INNER JOIN relation_test _RelationTest ON (modifier_test.relation_id=_RelationTest.id) ORDER BY _RelationTest.name ASC"
164+
);
167165
}
168166
}

0 commit comments

Comments
 (0)