diff --git a/src/extension/BaseRelationsTrait.php b/src/extension/BaseRelationsTrait.php index 0b0f514..29d16d0 100644 --- a/src/extension/BaseRelationsTrait.php +++ b/src/extension/BaseRelationsTrait.php @@ -8,6 +8,7 @@ use rjapi\blocks\FileManager; use rjapi\helpers\Classes; use rjapi\helpers\ConfigHelper; +use rjapi\helpers\Errors; use rjapi\helpers\Json; use rjapi\helpers\MigrationsHelper; use rjapi\types\DirsInterface; @@ -30,14 +31,7 @@ public function relations(Request $request, $id, string $relation) : Response { $model = $this->getEntity($id); if (empty($model)) { - Json::outputErrors( - [ - [ - JSONApiInterface::ERROR_TITLE => 'Database object ' . $this->entity . ' with $id = ' . $id . - ' - not found.', - ], - ] - ); + Json::outputErrors((new Errors())->getModelNotFound($this->entity, $id)); } $resource = Json::getRelations($model->$relation, $relation); @@ -86,18 +80,13 @@ private function presetRelations(Request $request, $id, string $relation) { $json = Json::decode($request->getContent()); $this->setRelationships($json, $id, true); + // set include for relations $_GET['include'] = $relation; $model = $this->getEntity($id); + if (empty($model)) { - Json::outputErrors( - [ - [ - JSONApiInterface::ERROR_TITLE => 'Database object ' . $this->entity . ' with $id = ' . $id . - ' - not found.', - ], - ] - ); + Json::outputErrors((new Errors())->getModelNotFound($this->entity, $id)); } return $model; @@ -255,6 +244,7 @@ private function saveModel(string $ucEntity, string $lowEntity, $eId, $rId) : vo { $relEntity = Classes::getModelEntity($ucEntity); $model = $this->getModelEntity($relEntity, $rId); + // swap table and field trying to find rels with inverse if (!property_exists($model, $lowEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID)) { $ucTmp = $ucEntity; @@ -267,6 +257,7 @@ private function saveModel(string $ucEntity, string $lowEntity, $eId, $rId) : vo $model->save(); return; } + $model->{$lowEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID} = $eId; $model->save(); } diff --git a/src/helpers/Errors.php b/src/helpers/Errors.php new file mode 100644 index 0000000..13faccb --- /dev/null +++ b/src/helpers/Errors.php @@ -0,0 +1,18 @@ + 'Database object ' . $entity . ' with $id = ' . $id . + ' - not found.', + ], + ]; + } +} \ No newline at end of file