Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Controls/ToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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]);
Expand Down
12 changes: 12 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down