Skip to content

Commit

Permalink
Added relations to QueryActionBase
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanNerd committed Aug 2, 2019
1 parent 71888b1 commit d5cccc1
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions app/Controllers/QueryActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class QueryActionBase extends ActionBase
*/
protected $allowAll = false;

/**
* @var array | null
*/
protected $relations = null;

/**
* Handle GET query request
*
Expand All @@ -37,13 +42,25 @@ public function __invoke(Request $request, Response $response, array $args): Res

$value = $args['value'];
$models = null;
$relations = $this->relations;

switch ($value) {
// SELECT *
case '*':
{
if ($this->allowAll) {
$models = $this->model->get()->all();
if ($relations === null) {
$models = $this
->model
->get()
->all();
} else {
$models = $this
->model
->with($relations)
->get()
->all();
}
}
break;
}
Expand All @@ -58,8 +75,14 @@ public function __invoke(Request $request, Response $response, array $args): Res
$model = $model->where($columnName, '=', $value);
}
}
$models = $model->get();

if ($relations === null) {
$models = $model->get();
} else {
$models = $model
->with($relations)
->get();
}
break;
}

Expand All @@ -68,7 +91,18 @@ public function __invoke(Request $request, Response $response, array $args): Res
{
$columnName = $parsedRequest['column_name'];
$operator = $parsedRequest['operator'] ?? '=';
$models = $this->model->where($columnName, $operator, $value)->get();
if ($relations === null) {
$models = $this
->model
->where($columnName, $operator, $value)
->get();
} else {
$models = $this
->model
->with($relations)
->where($columnName, $operator, $value)
->get();
}
}
}

Expand Down

0 comments on commit d5cccc1

Please sign in to comment.