public function addAction(Request $request) { $cateArray = array(); // $category= $this->get('doctrine')->getRepository('AcmeCategoryBundle:Category')->findBy(array('status'=>1)); // foreach($category as $key => $data): // $cateArray[$data->getId()] = $data->getName(); // endforeach; $user = $this->container->get('security.context')->getToken()->getUser(); $userId = $user->getId(); $userName = $user->getUsername(); if ($userName == 'admin') { $category = $this->get('doctrine')->getRepository('AcmeCategoryBundle:Category')->findAll(); } else { $category = $this->get('doctrine')->getRepository('AcmeCategoryBundle:Category')->findBy(array('userid' => $userId)); } foreach ($category as $key => $data) { $cateArray[$data->getId()] = $data->getName(); } $queue = $cateArray; array_unshift($queue, "select category"); $cate = $queue; $products = new \Acme\StoreBundle\Entity\Product(); $products->setCategory(''); $products->setName(''); $products->setPrice(''); $products->setDescription(''); $products->setImage(''); $form = $this->createFormBuilder($products)->add('category', 'choice', array('choices' => array('placeholder' => 'select category', '' => $cateArray)))->add('name', 'text')->add('price', 'text')->add('description', 'textarea')->add('image', 'file')->getForm(); $form->handleRequest($request); $validator = $this->get('validator'); $errors = $validator->validate($products); if (count($errors) > 0) { return $this->render('AcmeStoreBundle:Default:add.html.twig', array('form' => $form->createView())); } else { $a = $request->request->get('form'); $img = $form['image']->getData()->getClientOriginalName(); $products = new Product(); $products->setUserid($request->get('userid')); $products->setName($a['name']); $products->setCategory($a['category']); $products->setPrice($a['price']); $products->setDescription($a['description']); $products->setStatus('1'); $products->setImage($img); $em = $this->getDoctrine()->getManager(); $em->persist($products); $em->flush(); $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/products/' . $products->getId(); mkdir($dir); $form->get('image')->getData()->move($dir, $img); return $this->redirect($this->generateUrl('acme_store')); } return $this->render('AcmeStoreBundle:Default:add.html.twig', array('form' => $form->createView())); }
/** * {@inheritDoc} */ public function setUserid($userid) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setUserid', array($userid)); return parent::setUserid($userid); }