예제 #1
0
 /**
  * Page update product
  *
  * @param Request $request
  * @param string $shopname
  * @param int $id
  *
  * @Route("/{shopname}/addProducts/{id}", name="update_product")
  * @Method({"GET", "POST"})
  *
  * @return mixed
  */
 public function updateAction(Request $request, $shopname, $id)
 {
     $redis = $this->get("snc_redis.default");
     $em = $this->getDoctrine()->getManager();
     /* @var Product $product */
     $product = $em->getRepository("ShopProductBundle:Product")->findOneBy(["id" => $id]);
     $upload = $this->createForm(ProductImageType::class, $product);
     $form = $this->createForm(ProductType::class, $product, ['action' => $this->generateUrl('update_product', ['shopname' => $shopname, 'id' => $id]), 'method' => 'post']);
     $form->handleRequest($request);
     $images = json_decode($redis->get("product_image_" . $this->getUser()->getId()), 1);
     if ($form->isValid()) {
         if (count($images) > 0) {
             foreach ($images as $image) {
                 $productImage = new ProductImage();
                 $productImage->setProduct($product);
                 $productImage->setPath($image);
                 $product->addImage($productImage);
             }
             $redis->del("product_image_" . $this->getUser()->getId());
         }
         $em->flush();
         return $this->redirectToRoute('product_platform', ['id' => $id]);
     }
     if (count($images) == 0) {
         $images = $product->getImage()->toArray();
     }
     return $this->render('ShopProductBundle:Form:form.html.twig', ['upload' => $upload->createView(), 'form' => $form->createView(), 'shopname' => $shopname, 'images' => $images]);
 }
예제 #2
0
 /**
  * @ORM\PreFlush()
  */
 public function upload()
 {
     foreach ($this->file as $uploadedFile) {
         $productImage = new ProductImage();
         $productImage->setFile($uploadedFile);
         $productImage->preUpload();
         $productImage->upload();
         $productImage->setProduct($this);
         $this->addImage($productImage);
     }
 }