Skip to content

Commit 898be56

Browse files
committed
added support for foreign fields
1 parent 4fdc5c5 commit 898be56

File tree

4 files changed

+144
-18
lines changed

4 files changed

+144
-18
lines changed

src/SearchQueryParser/Parser.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,26 @@ private static function parseFullTextSearch($request,$modelClassName,$excludeFro
4040
$classList = RelationalMappingUtil::getClassConfiguration($modelClassName);
4141
foreach ($classList as $className => $class){
4242
foreach ($class['fields'] as $fieldName => $field){
43-
if(RelationalMappingUtil::_isGet($field) &&
44-
!RelationalMappingUtil::_isAutoIncrement($field) &&
43+
if(RelationalMappingUtil::_isGet($field) &&
44+
!RelationalMappingUtil::_isAutoIncrement($field) &&
4545
!RelationalMappingUtil::_isReference($field) &&
4646
!in_array($fieldName, $excludeFromFullTextSearch)){
47-
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $fieldName), $fullTextSearchQuery]);
47+
if(RelationalMappingUtil::_isForeignField($field)){
48+
$foreignClassAndField = preg_split("/\-\>/",$field['foreignField']);
49+
$foreignClassName = $foreignClassAndField[0];
50+
$foreignFieldName = $foreignClassAndField[1];
51+
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($foreignClassName, $foreignFieldName), $fullTextSearchQuery]);
52+
}else{
53+
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $fieldName), $fullTextSearchQuery]);
54+
}
4855
}
4956
}
5057
}
51-
$whereExpression = new Expression(Model::OPERATOR_OR, $fullTextSearchExpressions);
58+
if(count($fullTextSearchExpressions) == 1){
59+
$whereExpression = $fullTextSearchExpressions[0];
60+
}else if(count($fullTextSearchExpressions) > 1){
61+
$whereExpression = new Expression(Model::OPERATOR_OR, $fullTextSearchExpressions);
62+
}
5263
}
5364
return $whereExpression;
5465
}

tests/SearchQueryParser/Models/M1.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,28 @@ class M1 extends Model{
4141
*/
4242
private $password = null;
4343

44-
44+
/**
45+
* @columnName M3_ID
46+
* @type bigint
47+
* @set
48+
* @get
49+
*/
50+
private $m3Id = null;
51+
52+
/**
53+
* @columnName M3_ID
54+
* @type varchar
55+
* @get
56+
* @foreignField "PhpPlatform\\Tests\\SearchQueryParser\\Models\\M3->name"
57+
*/
58+
private $m3Name = null;
59+
60+
/**
61+
* @columnName M3_ID
62+
* @type varchar
63+
* @get
64+
* @foreignField "PhpPlatform\\Tests\\SearchQueryParser\\Models\\M3->phone"
65+
*/
66+
private $m3Phone = null;
67+
4568
}

tests/SearchQueryParser/Models/M3.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace PhpPlatform\Tests\SearchQueryParser\Models;
3+
4+
use PhpPlatform\Persist\Model;
5+
6+
/**
7+
* @tableName M3
8+
* @prefix m3
9+
*/
10+
class M3 extends Model{
11+
12+
/**
13+
* @columnName ID
14+
* @type bigint
15+
* @primary
16+
* @autoIncrement
17+
* @get
18+
*/
19+
private $id = null;
20+
21+
/**
22+
* @columnName NAME
23+
* @type varchar
24+
* @set
25+
* @get
26+
*/
27+
private $name = null;
28+
29+
/**
30+
* @columnName PHONE
31+
* @type varchar
32+
* @set
33+
* @get
34+
*/
35+
private $phone = null;
36+
37+
}

tests/SearchQueryParser/TestParser.php

+68-13
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,22 @@ function parseDataProvider(){
122122
null,
123123
['filters'=>['id'=>'1','m1Id'=>'2'],'sort'=>[],'pagination'=>null,'where'=>null]
124124
],
125-
125+
"with filters for foreign fields"=>[
126+
$this->getHttpRequestWithQueryParameters([
127+
'f'=>base64_encode(json_encode(['m3Name'=>'m3_Name']))
128+
]),
129+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
130+
null,
131+
['filters'=>['m3Name'=>'m3_Name'],'sort'=>[],'pagination'=>null,'where'=>null]
132+
],
133+
"with filters for child model with foreign fields"=>[
134+
$this->getHttpRequestWithQueryParameters([
135+
'f'=>base64_encode(json_encode(['m3Id'=>'2','m3Phone'=>['LIKE'=>'1234']]))
136+
]),
137+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
138+
null,
139+
['filters'=>['m3Id'=>'2','m3Phone'=>['LIKE'=>'1234']],'sort'=>[],'pagination'=>null,'where'=>null]
140+
],
126141
"with sort"=>[
127142
$this->getHttpRequestWithQueryParameters([
128143
's'=>base64_encode(json_encode(['name'=>'ASC']))
@@ -192,6 +207,22 @@ function parseDataProvider(){
192207
null,
193208
['filters'=>[],'sort'=>['id'=>'ASC','m1Id'=>'DESC'],'pagination'=>null,'where'=>null]
194209
],
210+
"with sort for foreign fields"=>[
211+
$this->getHttpRequestWithQueryParameters([
212+
's'=>base64_encode(json_encode(['m3Id'=>'ASC']))
213+
]),
214+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
215+
null,
216+
['filters'=>[],'sort'=>['m3Id'=>'ASC'],'pagination'=>null,'where'=>null]
217+
],
218+
"with sort for child class with foreign fields"=>[
219+
$this->getHttpRequestWithQueryParameters([
220+
's'=>base64_encode(json_encode(['m3Name'=>'ASC']))
221+
]),
222+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
223+
null,
224+
['filters'=>[],'sort'=>['m3Name'=>'ASC'],'pagination'=>null,'where'=>null]
225+
],
195226

196227
"with pagination"=>[
197228
$this->getHttpRequestWithQueryParameters([
@@ -252,21 +283,43 @@ function parseDataProvider(){
252283
]),
253284
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
254285
null,
255-
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%')"]
286+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
256287
],
257288
"with full text search for child class"=>[
258289
$this->getHttpRequestWithQueryParameters([
259290
'q'=>'abcd'
260291
]),
261292
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
262293
null,
263-
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m2.ADDRESS LIKE '%abcd%') OR (m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%')"]
264-
]
265-
266-
294+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m2.ADDRESS LIKE '%abcd%') OR (m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
295+
],
296+
"with full text search excluding a field"=>[
297+
$this->getHttpRequestWithQueryParameters([
298+
'q'=>'abcd'
299+
]),
300+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
301+
['name'],
302+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.USER_NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
303+
],
304+
"with full text search for child class and excluding a field"=>[
305+
$this->getHttpRequestWithQueryParameters([
306+
'q'=>'abcd'
307+
]),
308+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
309+
['userName','address'],
310+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
311+
],
312+
"with full text search for child class and excluding a foreign field"=>[
313+
$this->getHttpRequestWithQueryParameters([
314+
'q'=>'abcd'
315+
]),
316+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
317+
['userName','address','m3Id','m3Name'],
318+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
319+
],
267320

268321
];
269-
//return [$cases['with pagination wrong format 3']];
322+
//return [$cases['with filters for foreign fields']];
270323
return $cases;
271324
}
272325

@@ -288,12 +341,14 @@ private function getHttpRequestWithQueryParameters($queryParams){
288341
private function getColumnNameMappingForTestModels(){
289342
$mapping = array();
290343

291-
$classList = RelationalMappingUtil::getClassConfiguration('PhpPlatform\Tests\SearchQueryParser\Models\M2');
292-
293-
foreach ($classList as $className=>$class){
294-
$prefix = $class['prefix'];
295-
foreach ($class['fields'] as $fieldName=>$field){
296-
$mapping["$className::$fieldName"] = $prefix.'.'.$field['columnName'];
344+
foreach (['PhpPlatform\Tests\SearchQueryParser\Models\M2','PhpPlatform\Tests\SearchQueryParser\Models\M3'] as $_className){
345+
$classList = RelationalMappingUtil::getClassConfiguration($_className);
346+
347+
foreach ($classList as $className=>$class){
348+
$prefix = $class['prefix'];
349+
foreach ($class['fields'] as $fieldName=>$field){
350+
$mapping["$className::$fieldName"] = $prefix.'.'.$field['columnName'];
351+
}
297352
}
298353
}
299354
return $mapping;

0 commit comments

Comments
 (0)