entity relation save

This commit is contained in:
2024-11-21 13:00:21 +03:00
parent 860ea1a82d
commit 62eff81213
5 changed files with 90 additions and 1 deletions

View File

@ -53,7 +53,7 @@ class EntityRelation
return false;
}
public function removeFromEntityRelations(string $entity, string $property): bool
public static function removePropertyFromEntityRelations(string $entity, string $property): bool
{
$entity_relations_info = Option::where("key", "entity_relations")->first();
if ($entity_relations_info) {
@ -71,6 +71,42 @@ class EntityRelation
return false;
}
public static function removePropertyRelation(string $property): bool
{
$entity_relations_info = Option::where("key", "entity_relations")->first();
if ($entity_relations_info) {
$entity_relations = json_decode($entity_relations_info->value, true);
foreach ($entity_relations as $entity => $entity_relation){
if (in_array($property, $entity_relation)) {
$index = array_search($property, $entity_relation);
unset($entity_relations[$entity][$index]);
}
}
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save();
return true;
}
return false;
}
public static function removeEntityRelation(string $entity): bool
{
$entity_relations_info = Option::where("key", "entity_relations")->first();
if ($entity_relations_info) {
$entity_relations = json_decode($entity_relations_info->value, true);
if (isset($entity_relations[$entity])) {
unset($entity_relations[$entity]);
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save();
return true;
}
}
return false;
}
public function getEntityRelationsBySlug(string $slug)
{
$entityRelations = $this->getEntitiesRelations();
@ -129,4 +165,22 @@ class EntityRelation
}
}
}
public function saveEntityRelationBySlug(string $slug, string $entity, Request $request): void
{
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
$moduleClass->saveInputs($entity, $request);
}
}
public function saveEntityRelation(string $entity, Request $request): void
{
$relations = $this->getEntityRelationsBySlug($entity);
if ($relations){
foreach ($relations as $relation){
$this->saveEntityRelationBySlug($relation, $entity, $request);
}
}
}
}