Skip to content

Commit

Permalink
Merge pull request #19 from bakaphp/fix/model-relations
Browse files Browse the repository at this point in the history
set default relationKey to the main resource primaryKey
  • Loading branch information
kaioken authored Apr 11, 2019
2 parents c4152eb + 2da482d commit b2f3b04
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/QueryParserCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down

0 comments on commit b2f3b04

Please sign in to comment.