private function updateChilds($childs, $lists, $validIdByLevels, $level, $parentId)
 {
     /**
      *
      * Two cases :
      *
      * 1) Item has already an ID => We update the item
      * 2) Item has no ID, it is a new One, we have to create IT.
      *
      */
     $em = $this->container->get("doctrine")->getManager();
     foreach ($childs as $key => $child) {
         if (array_key_exists("id", $child)) {
             // Case of existing item, just update it.
             $item = $em->getRepository('TellawLeadsFactoryBundle:ReferenceListElement')->find($child["id"]);
             $item->setName($child["name"]);
             $item->setValue($child["value"]);
             // Get this to keep in memory treated items to delete old ones.
             $validIdByLevels[$lists[$level]][$child["id"]] = true;
         } else {
             // Case of new Item, create it
             $item = new ReferenceListElement();
             $item->setName($child["name"]);
             $item->setValue($child["value"]);
             $referenceList = $em->getRepository('TellawLeadsFactoryBundle:ReferenceList')->find($lists[$level]);
             $item->setReferenceList($referenceList);
             if ($parentId != null) {
                 $parent = $em->getRepository('TellawLeadsFactoryBundle:ReferenceListElement')->find($parentId);
                 $item->setParent($parent);
             }
             $em->persist($item);
             $validIdByLevels[$lists[$level]][$item->getId()] = true;
         }
         /**
          * Digging into childrens
          */
         $parentIdTmp = $item->getId();
         if (array_key_exists("childrens", $child)) {
             if (count($child["childrens"]) > 0) {
                 list($validIdByLevels, $lists) = $this->updateChilds($child["childrens"], $lists, $validIdByLevels, $level + 1, $parentIdTmp);
             }
         }
     }
     $em->flush();
     return array($validIdByLevels, $lists);
 }
 /**
  * @Route("/referenceList/updateElement", name="_referenceList_updateElement")
  * @Secure(roles="ROLE_USER")
  */
 public function updateElementAjaxAction(Request $request)
 {
     $listId = $request->request->get("listid");
     $id = $request->request->get("id");
     $text = $request->request->get("text");
     $value = $request->request->get("value");
     $enabled = $request->request->get("enabled");
     if (trim($id) != "" && $id != 0) {
         //var_dump("update");
         $em = $this->getDoctrine()->getManager();
         $element = $this->get('leadsfactory.reference_list_element_repository')->find($id);
         $element->setName($text);
         $element->setValue($value);
         $element->setStatus($enabled);
         $em->flush();
     } else {
         //var_dump("create");
         $list = $this->get('leadsfactory.reference_list_repository')->find($listId);
         $em = $this->getDoctrine()->getManager();
         $element = new ReferenceListElement();
         $element->setReferenceList($list);
         $element->setName($text);
         $element->setValue($value);
         $element->setStatus($enabled);
         $em->persist($element);
         //var_dump($element);
         $em->flush();
     }
     //var_dump($element);
     return new Response('Enregistré');
 }
 private function processTwoLevelElements($content)
 {
     $em = $this->getContainer()->get("doctrine")->getEntityManager();
     $list0 = $this->listsOrder[0];
     if (count($this->listsOrder) == 2) {
         $list1 = $this->listsOrder[1];
     }
     foreach ($content as $key => $item) {
         $element = new ReferenceListElement();
         $element->setReferenceList($this->lists[$list0]);
         $element->setName($item["name"]);
         $element->setValue($key);
         $element->setStatus(1);
         $em->persist($element);
         if (!isset($list1)) {
             continue;
         }
         foreach ($item["childrens"] as $children) {
             $childElement = new ReferenceListElement();
             $childElement->setReferenceList($this->lists[$list1]);
             $childElement->setParent($element);
             $childElement->setName($children["name"]);
             $childElement->setValue($children["value"]);
             $childElement->setStatus(1);
             $em->persist($childElement);
         }
     }
     echo "Fin de l'importation\n";
     $em->flush();
 }