public function save(Item $item, ArrayCollection $oldAttributes) { // add EntityManager to short variable $em = $this->getEntityManager(); // remove removed attributes // oldAttributes is ArrayCollection cannot // be casted to empty array if empty Helper::loopCollection(function ($collectionItem) use($item, $em) { if (false === $item->getAttributes()->contains($collectionItem)) { // delete attribute permanently $em->remove($collectionItem); } }, $oldAttributes); // associate attribute with item // attributes is persisted automatically Helper::loopCollection(function ($collectionItem) use($item) { $collectionItem->setItem($item); }, $item->getAttributes()); if (!$item->getId()) { // set data for new item $item->setCreatedAt(new \DateTime()); $item->setSlug(Helper::makeSlugs($item->getName())); } // set when was item updated then persist it // and flush to database $item->setUpdatedAt(new \DateTime()); $em->persist($item); $em->flush(); }
public function newAction(Request $request, $category_id = false, $type_id = false) { // get repository for category $category_repository = $this->em()->getRepository("ErikAppBundle:Category"); // get repository for type $type_repository = $this->em()->getRepository("ErikAppBundle:ItemType"); // if category is not set in url // than render page to select category if (!$category_id && !$type_id) { $categories = $category_repository->findBy(array("active" => true, "root" => null)); return new Response($this->renderView("::backend/pages/item/categoryChoose.html.twig", array("categories" => $categories))); } // if type is not set // than render page to select type that is in // selected category if ($category_id && !$type_id) { $types = $type_repository->getByCategory($category_id); return new Response($this->renderView("::backend/pages/item/typeChoose.html.twig", array("types" => $types, "category_id" => $category_id))); } // get the type $type = $type_repository->find($type_id); if (!$type) { $this->addFlash("notice", $this->get("translator")->trans("something.went.wrong")); return $this->redirect($this->generateUrl("erik_app_admin_item_list")); } // get what kind of category this type have // yes it was pre. selected but we need this // because with this we can additionally check // if id in url was modified by user to type ID // that has no category $category = $category_repository->find($category_id); // so if it has not category say good bye if (!$category) { $this->addFlash("notice", $this->get("translator")->trans("something.went.wrong")); return $this->redirect($this->generateUrl("erik_app_admin_item_list")); } // get repository for item entity $repository = $this->em()->getRepository("ErikAppBundle:Item"); // create new item $item = new Item(); // initialize new item form type to render and validate form $itemFormType = new ItemType(); // Create collection of the current attributes in the database // related to this item // Important!!! this needs to be before creating form otherwise // it will be replaced with submitted attributes $oldAttributes = new ArrayCollection(); if ($item->getAttributes()) { foreach ($item->getAttributes() as $attribute) { $oldAttributes->add($attribute); } } // create the form $form = $this->createForm($itemFormType, $item, array("itemtype_id" => $type->getId())); // handle http request $form->handleRequest($request); // check if form is submitted and is valid if ($form->isSubmitted() && $form->isValid()) { // set category manually because its not form element $item->setCategory($category); // set type manually because its also not element $item->setType($type); // save the changes $repository->save($item, $oldAttributes); if ($item->getId()) { // display success message $this->addFlash("success", $this->get("translator")->trans("item.has.been.saved")); return $this->redirectToRoute("erik_app_admin_item_edit", array("id" => $item->getId())); } else { // display error message $this->addFlash("danger", $this->get("translator")->trans("item.has.been.not.saved")); } } // return http response with rendered template return new Response($this->renderView("::backend/pages/item/edit.html.twig", array("form" => $form->createView(), "item" => $item))); }