/** * Clones an entire tree and persists to database * * @param BlockOwnerInterface $block * * @return BlockOwnerInterface */ public function duplicate(BlockOwnerInterface $block) { $this->killDraftVersionFilter(); $this->killLoggableListener(); $blocks = $this->findByOwner($block); array_unshift($blocks, $block); // 1. Interate over all owned blocks and disconnect parents keeping ids /** @var Block $descendant */ foreach ($blocks as $descendant) { $descendant->originalId = $descendant->getId(); // if it has a parent we need to put it somewhere if ($descendant->getParent()) { $descendant->originalParentId = $descendant->getParent()->getId(); $descendant->setParent(null); } if ($descendant instanceof BlockContainerInterface) { $descendant->setChildren(null); } $descendant->setOwner($block); $descendant->setRootVersion(0); $this->em->detach($descendant); $this->em->persist($descendant); } $this->em->flush(); // 2. Iterate over all new blocks and reset their parents foreach ($blocks as $descendant) { if (isset($descendant->originalParentId)) { foreach ($blocks as $parent) { if ($descendant->originalParentId === $parent->originalId) { $descendant->setParent($parent); $parent->addChild($descendant); } } } } $this->em->flush(); return $block; }
/** * For cloning purpose * * @param ContentInterface|\Opifer\EavBundle\Model\ValueSetInterface|\Opifer\EavBundle\Entity\Value $entity */ private function detachAndPersist($entity) { $this->em->detach($entity); $this->em->persist($entity); }