Example #1
0
 public function saveProductAction(Request $request)
 {
     // Получение входных параметров
     $title = $request->get('title');
     $price = $request->get('price');
     $videoUrl = $request->get('video');
     $images = $request->get('images');
     $description = $request->get('description');
     $categoryId = $request->get('category');
     $associatedProducts = $request->get('associatedProducts');
     $filterValues = $request->get('filterValues');
     $subcategories = $request->get('subcategories');
     $productId = $request->get('productId');
     if ($productId) {
         $product = $this->getDoctrine()->getRepository('ShopBundle:Product')->find($productId);
         if (!$product) {
             return new JsonResponse(['errors' => true, 'message' => 'Передан не корректный идентификатор продукта']);
         }
     } else {
         $product = new Product();
     }
     $product->setTitle($title)->setPrice($price)->setImages($images)->setVideo($videoUrl)->setDescription($description);
     $category = $this->getDoctrine()->getRepository('ShopBundle:Category')->find($categoryId);
     if (!$category) {
         return new JsonResponse(['errors' => true, 'message' => 'Передан не корректный идентификатор категории']);
     }
     $product->setCategory($category);
     if ($associatedProducts) {
         $associatedProducts = rtrim($associatedProducts, ',');
         $associatedProducts = explode(',', $associatedProducts);
         $associatedProducts = array_unique($associatedProducts);
         $product->setAssociatedProducts();
         foreach ($associatedProducts as $associatedProductId) {
             $associatedProduct = $this->getDoctrine()->getRepository('ShopBundle:Product')->find($associatedProductId);
             if (!$associatedProduct) {
                 return new JsonResponse(['errors' => true, 'message' => 'Передан не корректный идентификатор ассоциированного продукта']);
             }
             $product->addAssociatedProduct($associatedProduct);
         }
     }
     if ($filterValues) {
         $filterValues = rtrim($filterValues, ',');
         $filterValues = explode(',', $filterValues);
         $filterValues = array_unique($filterValues);
         $product->setFilterValues();
         foreach ($filterValues as $filterValueId) {
             $filterValue = $this->getDoctrine()->getRepository('ShopBundle:FilterValue')->find($filterValueId);
             if (!$filterValue) {
                 return new JsonResponse(['errors' => true, 'message' => 'Передан не корректный идентификатор фильтра']);
             }
             $product->addFilterValue($filterValue);
         }
     }
     if ($subcategories) {
         $subcategories = rtrim($subcategories, ',');
         $subcategories = explode(',', $subcategories);
         $subcategories = array_unique($subcategories);
         $product->setSubCategory();
         foreach ($subcategories as $subcategoryId) {
             $subcategory = $this->getDoctrine()->getRepository('ShopBundle:Category')->find($subcategoryId);
             if (!$subcategory) {
                 return new JsonResponse(['errors' => true, 'message' => 'Передан не корректный идентификатор подкатегории']);
             }
             $product->addSubCategory($subcategory);
         }
     }
     $em = $this->getDoctrine()->getEntityManager();
     $em->persist($product);
     $em->flush();
     return new JsonResponse(['errors' => false]);
 }