diff --git a/src/Api.php b/src/Api.php index e917eb5..f961e2f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -22,6 +22,7 @@ class Api /** @var string Request path */ public $path; + /** * Reads everything off globals. * @@ -178,7 +179,7 @@ protected function call($callable, $vars = []) */ protected function exportModel(\atk4\data\Model $m) { - return $m->export($this->getAllowedFields($m, 'read')); + return $m->export($this->getAllowedFields($m, 'read'), null, false); } /** @@ -405,12 +406,26 @@ public function rest($pattern, $model = null, $methods = null) $model = $this->call($model, $args); } - // limit fields - $model->onlyFields($this->getAllowedFields($model, 'read')); + $this->loadModelByValue($model, $id); + + //calculate only once + $allowed_fields = $this->getAllowedFields($model, 'read'); + $data = []; + // get all field-elements + foreach ($model->elements as $field => $f) { + //only use allowed fields + if(!in_array($field, $allowed_fields)) { + continue; + } + + if ($f instanceof \atk4\data\Field) { + $data[$field] = $f->toString(); + } + } - // load model and get field values - return $this->loadModelByValue($model, $id)->get(); + return $data; }; + $this->get($pattern.'/:id', $f); }