Skip to content

Commit 39e9313

Browse files
committed
fixed error in handling foreign fields in full test search
1 parent 898be56 commit 39e9313

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/SearchQueryParser/Parser.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ private static function parseFullTextSearch($request,$modelClassName,$excludeFro
4444
!RelationalMappingUtil::_isAutoIncrement($field) &&
4545
!RelationalMappingUtil::_isReference($field) &&
4646
!in_array($fieldName, $excludeFromFullTextSearch)){
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-
}
47+
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $fieldName), $fullTextSearchQuery]);
5548
}
5649
}
5750
}

tests/SearchQueryParser/TestParser.php

+21-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpPlatform\Errors\Exceptions\Http\_4XX\BadRequest;
99
use PhpPlatform\Persist\RelationalMappingUtil;
1010
use PhpPlatform\Persist\TransactionManager;
11+
use PhpPlatform\Persist\RelationalMappingCache;
1112

1213
class TestParser extends \PHPUnit_Framework_TestCase{
1314

@@ -283,39 +284,39 @@ function parseDataProvider(){
283284
]),
284285
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
285286
null,
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%')"]
287+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3_M3_ID.NAME LIKE '%abcd%') OR (m3_M3_ID.PHONE LIKE '%abcd%')"]
287288
],
288289
"with full text search for child class"=>[
289290
$this->getHttpRequestWithQueryParameters([
290291
'q'=>'abcd'
291292
]),
292293
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
293294
null,
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+
['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_M3_ID.NAME LIKE '%abcd%') OR (m3_M3_ID.PHONE LIKE '%abcd%')"]
295296
],
296297
"with full text search excluding a field"=>[
297298
$this->getHttpRequestWithQueryParameters([
298299
'q'=>'abcd'
299300
]),
300301
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
301302
['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+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.USER_NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3_M3_ID.NAME LIKE '%abcd%') OR (m3_M3_ID.PHONE LIKE '%abcd%')"]
303304
],
304305
"with full text search for child class and excluding a field"=>[
305306
$this->getHttpRequestWithQueryParameters([
306307
'q'=>'abcd'
307308
]),
308309
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
309310
['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+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m1.M3_ID LIKE '%abcd%') OR (m3_M3_ID.NAME LIKE '%abcd%') OR (m3_M3_ID.PHONE LIKE '%abcd%')"]
311312
],
312313
"with full text search for child class and excluding a foreign field"=>[
313314
$this->getHttpRequestWithQueryParameters([
314315
'q'=>'abcd'
315316
]),
316317
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
317318
['userName','address','m3Id','m3Name'],
318-
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m3.PHONE LIKE '%abcd%')"]
319+
['filters'=>[],'sort'=>[],'pagination'=>null,'where'=>"(m1.NAME LIKE '%abcd%') OR (m3_M3_ID.PHONE LIKE '%abcd%')"]
319320
],
320321

321322
];
@@ -341,14 +342,22 @@ private function getHttpRequestWithQueryParameters($queryParams){
341342
private function getColumnNameMappingForTestModels(){
342343
$mapping = array();
343344

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'];
345+
$classList = RelationalMappingUtil::getClassConfiguration('PhpPlatform\Tests\SearchQueryParser\Models\M2');
346+
347+
foreach ($classList as $className=>$class){
348+
$prefix = $class['prefix'];
349+
foreach ($class['fields'] as $fieldName=>$field){
350+
$columnName = $field['columnName'];
351+
if(RelationalMappingUtil::_isForeignField($field)){
352+
$foreignClassAndField = preg_split("/\-\>/",$field['foreignField']);
353+
$foreignClassName = $foreignClassAndField[0];
354+
$foreignFieldName = $foreignClassAndField[1];
355+
$foreignClassConf = RelationalMappingCache::getInstance()->get($foreignClassName);
356+
357+
$prefix = $foreignClassConf['prefix']."_".$field['columnName'];
358+
$columnName = $foreignClassConf['fields'][$foreignFieldName]['columnName'];
351359
}
360+
$mapping["$className::$fieldName"] = $prefix.'.'.$columnName;
352361
}
353362
}
354363
return $mapping;

0 commit comments

Comments
 (0)