diff --git a/src/Entity/Base/AbstractStructuralDBElement.php b/src/Entity/Base/AbstractStructuralDBElement.php index b784ab2a..4384173f 100644 --- a/src/Entity/Base/AbstractStructuralDBElement.php +++ b/src/Entity/Base/AbstractStructuralDBElement.php @@ -322,6 +322,11 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement $this->parent = $new_parent; + //Add this element as child to the new parent + if (null !== $new_parent) { + $new_parent->getChildren()->add($this); + } + return $this; } @@ -340,18 +345,28 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement } /** - * @param static[]|Collection $elements - * + * Adds the given element as child to this element. + * @param static $child * @return $this */ - public function setChildren($elements): self + public function addChild(self $child): self { - if (!is_array($elements) && !$elements instanceof Collection) { - throw new InvalidArgumentException('$elements must be an array or Collection!'); - } - - $this->children = $elements; + $this->children->add($child); + //Children get this element as parent + $child->setParent($this); + return $this; + } + /** + * Removes the given element as child from this element. + * @param static $child + * @return $this + */ + public function removeChild(self $child): self + { + $this->children->removeElement($child); + //Children has no parent anymore + $child->setParent(null); return $this; } diff --git a/src/Form/Type/Helper/StructuralEntityChoiceLoader.php b/src/Form/Type/Helper/StructuralEntityChoiceLoader.php index 409b8837..6bca91b4 100644 --- a/src/Form/Type/Helper/StructuralEntityChoiceLoader.php +++ b/src/Form/Type/Helper/StructuralEntityChoiceLoader.php @@ -54,26 +54,6 @@ class StructuralEntityChoiceLoader extends AbstractChoiceLoader return array_merge($tmp, $this->builder->typeToNodesList($this->options['class'], null)); } - /*public function loadChoicesForValues(array $values, callable $value = null) - { - $tmp = parent::loadChoicesForValues($values, $value); - - if ($this->options['allow_add'] && empty($tmp)) { - if (count($values) > 1) { - throw new \InvalidArgumentException('Cannot add multiple entities at once.'); - } - - //Dont create a new entity for the empty option - if ($values[0] === "" || $values[0] === null) { - return $tmp; - } - - return [$this->createNewEntitiesFromValue($values[0])[0]]; - } - - return $tmp; - }*/ - public function createNewEntitiesFromValue(string $value): array { if (!$this->options['allow_add']) {