示例#1
0
 /**
  * Creates a new Product entity.
  *
  */
 public function createAction(Request $request)
 {
     $category_id = $request->request->get('minishop_shopbundle_product')['category'];
     $category = $this->getCategory($category_id);
     $product = new Product();
     $product->setCategory($category);
     $form = $this->createForm(new ProductType(), $product);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->persist($product);
         $em->flush();
         $response = new Response(json_encode(array('id' => $product->getId(), 'name' => $product->getName(), 'brand' => $product->getBrand(), 'description' => $product->getDescription(), 'price' => $product->getPrice(), 'category' => $product->getCategory()->getName(), 'product_link' => $this->generateUrl('admin_product_show', array('id' => $product->getId())), 'product_edit_link' => "'" . $this->generateUrl('admin_product_edit', array('id' => $product->getId())) . "'")));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
         //      if($request->isXmlHttpRequest()) {
         //      } else {
         //        return $this->redirect($this->generateUrl('admin_product_show', array('id' => $product->getId())));
         //      }
     }
     return $this->render('MinishopBackendBundle:Product:new.html.twig', array('entity' => $product, 'form' => $form->createView()));
 }