コード例 #1
0
ファイル: ProductController.php プロジェクト: m7moud/zendcart
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('product', array('action' => 'add'));
     }
     try {
         $product = $this->getProductTable()->getProduct($id);
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('product', array('action' => 'index'));
     }
     $form = new ProductForm();
     $form->bind($product);
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($product->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getProductTable()->saveProduct($product);
             return $this->redirect()->toRoute('product');
         }
     }
     return array('id' => $id, 'form' => $form);
 }
コード例 #2
0
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('product', array('action' => 'add'));
     }
     // Get the Product with the specified id.  An exception is thrown
     // if it cannot be found, in which case go to the index page.
     try {
         $product = $this->getProductTable()->getProduct($id);
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('product', array('action' => 'index'));
     }
     if (!$product) {
         return $this->redirect()->toRoute('product', array('action' => 'add'));
     }
     // var_dump($product);
     $form = new ProductForm();
     $form->bind($product);
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($product->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getProductTable()->saveProduct($product);
             // Redirect to list of products
             return $this->redirect()->toRoute('product');
         }
     }
     return array('id' => $id, 'form' => $form, 'shops' => $this->getShopTable()->fetchAll($this->getServiceLocator()->get('AuthService')->getStorage()->read()->id), 'product_categories' => $this->getProductCategoryTable()->fetchAll($this->getServiceLocator()->get('AuthService')->getStorage()->read()->id));
 }
コード例 #3
0
 public function editAction()
 {
     $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_another');
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('product', array('action' => 'create'));
     }
     try {
         $product = $em->getRepository("Product\\Entity\\Product")->find($id);
         $productModel = new \Product\Model\Product();
         $productModel->exchangeProductEntity($product);
         $oldFileName = $product->getImage();
         $tag = new Tags();
         foreach ($product->getIdTag() as $tag) {
             $tagArrayName[] = $tag->getName();
         }
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('product', array('action' => 'list'));
     }
     $form = new ProductForm();
     $form->bind($productModel);
     $form->get('tags')->setValue(implode(",", $tagArrayName));
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($productModel->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $File = $this->params()->fromFiles('image');
             $product->exchangeArray($request->getPost());
             $adapter = new \Zend\File\Transfer\Adapter\Http();
             $this->deleteImage($oldFileName);
             $this->saveImage($form, $adapter, $File);
             $product->setImage($adapter->getFileName(null, false));
             foreach ($product->getIdTag() as $tag) {
                 $em->remove($tag);
             }
             $tagsArray = explode(",", $form->get("tags")->getValue());
             foreach ($tagsArray as $tag) {
                 $entityTag = new Tags();
                 $entityTag->setName($tag);
                 $product->getIdTag()->add($entityTag);
                 $em->persist($entityTag);
             }
             $em->persist($product);
             $em->flush();
             return $this->redirect()->toRoute('product');
         }
     }
     return array('id' => $id, 'form' => $form);
 }