update, delete entity relations

This commit is contained in:
2024-11-28 15:34:57 +03:00
parent 44e61a2030
commit 921569b950
4 changed files with 45 additions and 5 deletions

View File

@ -203,4 +203,22 @@ class EntityRelation
return [];
}
public function deleteEntityRelationBySlug(string $slug, string $entity, Model $model): void
{
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
if ($moduleClass and method_exists($moduleClass, "deleteItems")) {
$moduleClass->deleteItems($entity, $model);
}
}
public function deleteEntityRelation(string $entity, Model $model): void
{
$relations = $this->getEntityRelationsBySlug($entity);
if ($relations){
foreach ($relations as $relation){
$this->deleteEntityRelationBySlug($relation, $entity, $model);
}
}
}
}

View File

@ -93,6 +93,10 @@ class PostController extends AdminController
$postForm->load($_REQUEST);
if ($postForm->validate()) {
$post = $this->postService->update($postForm, $post);
$entityRelation = new EntityRelation();
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
if ($post) {
$this->redirect("/admin/post/view/" . $post->id);
}
@ -109,6 +113,10 @@ class PostController extends AdminController
if (!$post){
throw new Exception(message: "The post not found");
}
$entityRelation = new EntityRelation();
$entityRelation->deleteEntityRelation(entity: "post", model: $post);
$post->delete();
$this->redirect("/admin/post/");
}