Skip to content

Commit

Permalink
Merge pull request #9 from draconusdesigns/master
Browse files Browse the repository at this point in the history
Adding Contains query string param
  • Loading branch information
xiaohutai committed Feb 5, 2016
2 parents da47eab + 8fbe70d commit e2e1c5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
19 changes: 19 additions & 0 deletions Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ public function jsonapi_list(Request $request, $contenttype)
}
}

// Handle $contains[], this modifies the $where[] clause to search using Like.
if ($contains = $request->get('contains')) {
foreach ($contains as $key => $value) {
if (!in_array($key, $allFields)) {
return $this->responseInvalidRequest([
'detail' => "Parameter [$key] does not exist for contenttype with name [$contenttype]."
]);
}

$values = explode(",", $value);

foreach ($values as $i => $item) {
$values[$i] = '%' . $item .'%';
}

$where[$key] = implode(' || ', $values);
}
}

// If `returnsingle` is not set to false, then a single result will not
// result in an array.
$where['returnsingle'] = false;
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ parameters appended to the URL):
|`include` | Fetches all the related records of the specified contenttype(s) of the record(s) in the `included` field of the JSON response. Separate multiple contenttypes with a comma. Example: `include=pages`. |
|`fields[]` | Set the fields that are shown in the response per specified contenttype. Separate multiple fields with commas. Multiple `fields[]` parameters are possible. Example: `fields[entries]=slug,teaser`. |
|`filter[]` | Filter records where a certain field must be equal to the specified `{value}`. Multiple `filter[]` parameters are possible. Example: `filter[id]=1,2`. |
|`contains[]` | Filter records where a certain field must contain the specified `{value}`. Multiple `contains[]` parameters are possible. Example: `contains[id]=1,2`. |


### Additional Queries
Expand Down

0 comments on commit e2e1c5f

Please sign in to comment.