diff --git a/src/Controls/ToMany.php b/src/Controls/ToMany.php index b57cba8..7fa8d18 100644 --- a/src/Controls/ToMany.php +++ b/src/Controls/ToMany.php @@ -73,8 +73,14 @@ public function load(ClassMetadata $meta, Component $component, $entity): bool $em = $this->mapper->getEntityManager(); $UoW = $em->getUnitOfWork(); + $collectionFilter = $this->mapper->getForm()->getComponentCollectionFilter($component); foreach ($collection as $key => $relation) { + // entities excluded by the collection filter are not managed by this container + if ($collectionFilter && !$collectionFilter($relation)) { + continue; + } + // mapuj jen pri neodeslanem formulari nebo pokud nebyl radek odstranen uzivatelem if (!$component->form->isSubmitted() || isset($component->getUntrustedValues('array')[$key])) { if ($UoW->getSingleIdentifierValue($relation)) { @@ -159,12 +165,18 @@ public function save(ClassMetadata $meta, Component $component, $entity): bool } $deletedCollectionEntityListener = $this->getDeletedCollectionEntityListener(); + $collectionFilter = $this->mapper->getForm()->getComponentCollectionFilter($component); foreach ($collection as $key => $relation) { if ($collection[$key]->isNew()) { continue; } + // entities excluded by the collection filter are not managed by this container, so they must not be deleted either + if ($collectionFilter && !$collectionFilter($relation)) { + continue; + } + if (!in_array((string) $key, $received)) { $deletedCollectionEntityListener?->addDeletedEntity($collection[$key]); unset($collection[$key]); diff --git a/src/Form.php b/src/Form.php index 8cce09f..b3f653d 100644 --- a/src/Form.php +++ b/src/Form.php @@ -16,6 +16,7 @@ class Form extends \ADT\Forms\Form protected array $componentFormMappers = []; protected array $componentEntityMappers = []; protected array $componentEntityFactories = []; + protected array $componentCollectionFilters = []; public function getEntityManager(): ?EntityManagerInterface { @@ -95,6 +96,17 @@ public function setComponentEntityMapper(IComponent $component, Closure $entityM return $this; } + public function getComponentCollectionFilter(IComponent $component): ?Closure + { + return $this->componentCollectionFilters[spl_object_hash($component)] ?? null; + } + + public function setComponentCollectionFilter(IComponent $component, Closure $collectionFilter): self + { + $this->componentCollectionFilters[spl_object_hash($component)] = $collectionFilter; + return $this; + } + public function getComponentEntityFactory(IComponent $component): ?Closure { return $this->componentEntityFactories[spl_object_hash($component)] ?? null;