From 2da482d1e0bc5e22465f0c66287aa08b8b9dec4d Mon Sep 17 00:00:00 2001 From: freesgen Date: Thu, 11 Apr 2019 16:40:53 -0400 Subject: [PATCH] set default relationKey to the main resource primaryKey - **WHY** the way manager->getRalationBetween works depends on how we declared the relation - **HOW** we'll let the old behavior work if not relations are found - **SIDE EFFECTS** none - **ISSUE LINK** --- src/QueryParserCustomFields.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/QueryParserCustomFields.php b/src/QueryParserCustomFields.php index ff73fc4..bde192f 100755 --- a/src/QueryParserCustomFields.php +++ b/src/QueryParserCustomFields.php @@ -257,10 +257,17 @@ protected function prepareCustomSearch($hasSubquery = false): array $relatedKey = $metaData->getPrimaryKeyAttributes($modelObject)[0]; $relation = $this->model->getModelsManager()->getRelationsBetween(get_class($this->model), get_class($modelObject)); - $relationKey = (isset($relation) && count($relation)) ? $relation[0]->getFields() : $relatedKey; + + $relationKey = $primaryKey; + $referenceKey = $primaryKey; + + if (isset($relation) && $relation && count($relation)) { + $relationKey = $relation[0]->getFields(); + $referenceKey = $relation[0]->getReferencedFields(); + } $sql .= " INNER JOIN {$model} ON {$model}.{$relatedKey} = ("; - $sql .= "SELECT {$model}.{$relatedKey} FROM {$model} WHERE {$model}.{$relatedKey} = {$classname}.{$relationKey}"; + $sql .= "SELECT {$model}.{$relatedKey} FROM {$model} WHERE {$model}.{$referenceKey} = {$classname}.{$relationKey}"; foreach ($searchFields as $fKey => $searchFieldValues) { if (is_array(current($searchFieldValues))) {