コード例 #1
0
 public function editAction(Request $request)
 {
     $id = $request->get('id');
     $em = $this->getDoctrine()->getManager();
     $user = $em->getRepository('AcmeUserBundle:User')->find($id);
     $products = new \Acme\UserBundle\Entity\User();
     $products->setName('');
     $products->setPrice('');
     $products->setDescription('');
     $products->setImage('');
     $form = $this->createFormBuilder($products)->add('name', 'text')->add('price', 'text')->add('description', 'textarea')->add('image', 'file', array('error_bubbling' => TRUE))->getForm();
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $errors = $validator->validate($products);
     if ($request->getMethod() == 'POST') {
         if (count($errors) > 1) {
             return $this->render('AcmeStoreBundle:Default:edit.html.twig', array('form' => $form->createView(), 'products' => $products, 'product' => $product, 'id' => $id, 'image' => $oldimg));
         } else {
             $a = $request->request->get('form');
             $products = $em->getRepository('AcmeStoreBundle:Product')->find($id);
             if ($_FILES['form']['name']['image'] != NULL) {
                 $products->setImage($_FILES['form']['name']['image']);
                 if (is_file($_SERVER['DOCUMENT_ROOT'] . '/uploads/products/' . $id . '/' . $oldimg)) {
                     unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/products/' . $id . '/' . $oldimg);
                     rmdir($_SERVER['DOCUMENT_ROOT'] . '/uploads/products/' . $id);
                     $img = $form['image']->getData()->getClientOriginalName();
                     $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/products/' . $products->getId();
                     mkdir($dir);
                     $form->get('image')->getData()->move($dir, $img);
                 }
             } else {
                 $products->setImage($oldimg);
             }
             $products->setName($a['name']);
             $products->setPrice($a['price']);
             $products->setDescription($a['description']);
             $products->setStatus('1');
             $em->persist($products);
             $em->flush();
         }
         return $this->redirect($this->generateUrl('acme_store'));
     }
     return $this->render('AcmeStoreBundle:Default:edit.html.twig', array('form' => $form->createView(), 'products' => $products, 'product' => $product, 'id' => $id, 'image' => $oldimg));
 }