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

@ -61,10 +61,19 @@ class TagModule extends Module
public function formInputs(string $entity, Model $model = null): void public function formInputs(string $entity, Model $model = null): void
{ {
if (isset($model)) {
$tags= TagEntity::where("entity_id", $model->id)->get()->toArray();
$value = [];
foreach ($tags as $tag) {
$val = Tag::where('id', $tag['tag_id'])->first()->toArray();
$value[] = $val['label'];
}
}
$input = SelectBuilder::build("tag[]", [ $input = SelectBuilder::build("tag[]", [
'class' => 'form-control', 'class' => 'form-control',
'placeholder' => 'Теги', 'placeholder' => 'Теги',
'value' => '', 'value' => $value ?? '',
'multiple' => "multiple", 'multiple' => "multiple",
'options' => Tag::getTagLabelByEntity($entity) 'options' => Tag::getTagLabelByEntity($entity)
]); ]);
@ -97,4 +106,9 @@ class TagModule extends Module
return substr($tagsStr, 0, -2); return substr($tagsStr, 0, -2);
} }
public function deleteItems(string $entity, Model $model): void
{
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
}
} }

8
composer.lock generated
View File

@ -1217,11 +1217,11 @@
}, },
{ {
"name": "itguild/forms", "name": "itguild/forms",
"version": "0.1.4", "version": "0.1.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.itguild.info/ItGuild/forms_bundle.git", "url": "https://git.itguild.info/ItGuild/forms_bundle.git",
"reference": "ddb17cc47360910b3875b88e10e14f91fcd875be" "reference": "45e57367d3f9571fde2b82fa2fd8126469ba6db6"
}, },
"require": { "require": {
"itguild/php-cg-select-v2": "^0.1.0", "itguild/php-cg-select-v2": "^0.1.0",
@ -1245,7 +1245,7 @@
"email": "apuc06@mail.ru" "email": "apuc06@mail.ru"
} }
], ],
"time": "2024-09-10T08:41:16+00:00" "time": "2024-11-28T10:18:58+00:00"
}, },
{ {
"name": "itguild/php-cg-select-v2", "name": "itguild/php-cg-select-v2",
@ -3205,5 +3205,5 @@
"ext-zip": "*" "ext-zip": "*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

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