/** * {@inheritDoc} */ public function setCategoryId($categoryId) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setCategoryId', array($categoryId)); return parent::setCategoryId($categoryId); }
/** * @Route("/product/set_category") * @Method({"POST", "GET"}) * */ public function setProductCategoryAction(Request $request) { $db = $this->getDoctrine()->getManager(); try { # 1. get product $product = $db->getRepository('ProductBundle:Product')->findOneById($request->get('product_id')); # 2. get category $category = $db->getRepository('ProductBundle:Category')->findOneById($request->get('category_id')); # 3. add product to category $product_category = new Product_Category(); $product_category->setProductId($product->getId()); $product_category->setCategoryId($category->getId()); $db->persist($product_category); $db->flush(); $message = 'New category was added for current product'; } catch (\Exception $e) { $message = 'Error: ' . $e->getMessage(); } return new JsonResponse(array('message' => $message)); }