public function process() { $nodes = array(); foreach ($this->data->nodes as $publicId => $nodeData) { $nodes[$publicId] = new Node(); $nodes[$publicId]->setPublicId($publicId); $nodes[$publicId]->setTreeVersion($this->treeVersion); $nodes[$publicId]->setTitle($nodeData->title); $nodes[$publicId]->setTitleAdmin($nodeData->title_admin); $nodes[$publicId]->setTitlePreviousAnswers($nodeData->title_previous_answers); $nodes[$publicId]->setBodyHTML($nodeData->body_html); $nodes[$publicId]->setBodyText($nodeData->body_text); $this->doctrine->persist($nodes[$publicId]); } foreach ($this->data->nodeOptions as $publicId => $nodeOptionData) { $nodeOption = new NodeOption(); $nodeOption->setPublicId($publicId); $nodeOption->setTreeVersion($this->treeVersion); $nodeOption->setTitle($nodeOptionData->title); $nodeOption->setBodyHTML($nodeOptionData->body_html); $nodeOption->setBodyText($nodeOptionData->body_text); $nodeOption->setSort($nodeOptionData->sort); $nodeOption->setNode($nodes[$nodeOptionData->node->id]); $nodeOption->setDestinationNode($nodes[$nodeOptionData->destination_node->id]); $this->doctrine->persist($nodeOption); } // If this is removed the tests will pass ... but the web app will crash. $this->doctrine->flush(); if ($this->data->start_node && $this->data->start_node->id) { $startingNode = new TreeVersionStartingNode(); $startingNode->setTreeVersion($this->treeVersion); $startingNode->setNode($nodes[$this->data->start_node->id]); $this->doctrine->persist($startingNode); } // Seeing as we have to flush in this function, also flush at end to make sure all written. $this->doctrine->flush(); }
public function go() { // Tree Option $this->newVersion->setFromOldVersion($this->oldVersion); // As we call flush in this function, make sure new version will definetly be part of that. $this->doctrine->persist($this->newVersion); // repos $nodeRepo = $this->doctrine->getRepository('QuestionKeyBundle:Node'); $nodeOptionRepo = $this->doctrine->getRepository('QuestionKeyBundle:NodeOption'); $libraryContentRepo = $this->doctrine->getRepository('QuestionKeyBundle:LibraryContent'); $nodeHasLibraryContentRepo = $this->doctrine->getRepository('QuestionKeyBundle:NodeHasLibraryContent'); $treeStartingNodeRepo = $this->doctrine->getRepository('QuestionKeyBundle:TreeVersionStartingNode'); // Library Content $libraryContents = array(); foreach ($libraryContentRepo->findByTreeVersion($this->oldVersion) as $libraryContent) { $libraryContents[$libraryContent->getPublicId()] = new LibraryContent(); $libraryContents[$libraryContent->getPublicId()]->setTitleAdmin($libraryContent->getTitleAdmin()); $libraryContents[$libraryContent->getPublicId()]->setBodyHTML($libraryContent->getBodyHTML()); $libraryContents[$libraryContent->getPublicId()]->setBodyText($libraryContent->getBodyText()); $libraryContents[$libraryContent->getPublicId()]->setTreeVersion($this->newVersion); $libraryContents[$libraryContent->getPublicId()]->setPublicId($libraryContent->getPublicId()); $this->doctrine->persist($libraryContents[$libraryContent->getPublicId()]); } // Nodes $nodes = $nodeRepo->findByTreeVersion($this->oldVersion); $newNodes = array(); foreach ($nodes as $node) { $newNodes[$node->getId()] = new Node(); $newNodes[$node->getId()]->setTreeVersion($this->newVersion); $newNodes[$node->getId()]->setTitle($node->getTitle()); $newNodes[$node->getId()]->setBodyText($node->getBodyText()); $newNodes[$node->getId()]->setBodyHTML($node->getBodyHTML()); $newNodes[$node->getId()]->setPublicId($node->getPublicId()); $newNodes[$node->getId()]->setFromOldVersion($node); $this->doctrine->persist($newNodes[$node->getId()]); $sort = 0; foreach ($libraryContentRepo->findForNode($node) as $oldLibraryContent) { // We create a NodeHasLibraryContent record directly instead of using // $nodeHasLibraryContentRepo->addLibraryContentToNode($libraryContents[$oldLibraryContent->getPublicId()], $newNodes[$node->getId()]); // because it's much more efficient in DB queries. Also, given some records might not have saved I'm not sure addLibraryContentToNode() would find the correct sort values anyway. $nodeHasLibraryContent = new NodeHasLibraryContent(); $nodeHasLibraryContent->setLibraryContent($libraryContents[$oldLibraryContent->getPublicId()]); $nodeHasLibraryContent->setNode($newNodes[$node->getId()]); $nodeHasLibraryContent->setSort($sort++); $this->doctrine->persist($nodeHasLibraryContent); } } // Node Options $nodeOptions = $nodeOptionRepo->findAllNodeOptionsForTreeVersion($this->oldVersion); foreach ($nodeOptions as $nodeOption) { $newNodeOption = new NodeOption(); $newNodeOption->setTreeVersion($this->newVersion); $newNodeOption->setNode($newNodes[$nodeOption->getNode()->getId()]); $newNodeOption->setDestinationNode($newNodes[$nodeOption->getDestinationNode()->getId()]); $newNodeOption->setTitle($nodeOption->getTitle()); $newNodeOption->setBodyHTML($nodeOption->getBodyHTML()); $newNodeOption->setBodyText($nodeOption->getBodyText()); $newNodeOption->setSort($nodeOption->getSort()); $newNodeOption->setPublicId($nodeOption->getPublicId()); $newNodeOption->setFromOldVersion($nodeOption); $this->doctrine->persist($newNodeOption); } // Tree Start $treeStartingNode = $treeStartingNodeRepo->findOneByTreeVersion($this->oldVersion); if ($treeStartingNode) { // we have to flush at this point otherwise when we try and persist at the next stage you get an error. $this->doctrine->flush(); $newTreeVersionStartingNode = new TreeVersionStartingNode(); $newTreeVersionStartingNode->setTreeVersion($this->newVersion); $newTreeVersionStartingNode->setNode($newNodes[$treeStartingNode->getNode()->getId()]); $this->doctrine->persist($newTreeVersionStartingNode); } // final flush $this->doctrine->flush(); }
public function newOptionAction($treeId, $versionId, $nodeId) { // build $return = $this->build($treeId, $versionId, $nodeId); if (!$this->treeVersionEditable) { throw new AccessDeniedException(); } //data $doctrine = $this->getDoctrine()->getManager(); $nodeOptionRepo = $doctrine->getRepository('QuestionKeyBundle:NodeOption'); $form = $this->createForm(new AdminNodeOptionNewType($this->node, $nodeOptionRepo->getNextSortValueForNode($this->node))); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $form->handleRequest($request); if ($form->isValid()) { $nodeOption = new NodeOption(); $nodeOption->setTreeVersion($this->treeVersion); $nodeOption->setNode($this->node); $nodeOption->setTitle($form->get('title')->getData()); $nodeOption->setBodyText($form->get('body_text')->getData()); $nodeOption->setBodyHTML($form->get('body_html')->getData()); $nodeOption->setSort($form->get('sort')->getData()); $nodeOption->setDestinationNode($form->get('destination_node')->getData()); $doctrine->persist($nodeOption); $doctrine->flush(); return $this->redirect($this->generateUrl('questionkey_admin_tree_version_node_show', array('treeId' => $this->tree->getPublicId(), 'versionId' => $this->treeVersion->getPublicId(), 'nodeId' => $this->node->getPublicId()))); } } return $this->render('QuestionKeyBundle:AdminTreeVersionNodeEdit:newOption.html.twig', array('form' => $form->createView(), 'tree' => $this->tree, 'treeVersion' => $this->treeVersion, 'node' => $this->node)); }