Exemple #1
0
 /**
  * @throw Markzero\Http\Exception\ResourceNotFoundException
  *        Markzero\Validation\Exception\ValidationException
  */
 public static function update($id, ParameterBag $params)
 {
     $em = self::getEntityManager();
     $product = Product::find($id);
     self::validateParams($params, $product);
     if ($product === null) {
         throw new ResourceNotFoundException();
     }
     $product->name = $params->get('product[name]', '', true);
     $product->short_desc = $params->get('product[short_desc]', '', true);
     $product->description = $params->get('product[description]', '', true);
     $product->price = floatval($params->get('product[price]', 0, true));
     $product->updated_at = new \DateTime("now");
     $isbn10 = $params->get('product[isbn_10]', null, true);
     $isbn13 = $params->get('product[isbn_13]', null, true);
     $issn = $params->get('product[issn]', null, true);
     $product->updateBarcode(Barcode::ISBN_10, $isbn10);
     $product->updateBarcode(Barcode::ISBN_13, $isbn13);
     $product->updateBarcode(Barcode::ISSN, $issn);
     $catid = $params->get('product[category_id]', null, true);
     $cat = Category::find($catid);
     if ($cat === null) {
         throw new ResourceNotFoundException('Category cannot be found');
     }
     $product->category = $cat;
     $em->persist($product);
     $em->flush();
     return $product;
 }
 function delete($id)
 {
     try {
         Product::delete($id);
         $this->respondTo('html', function () {
             $this->getResponse()->redirect('App\\Admin\\Controllers\\ProductController', 'index');
         });
     } catch (ResourceNotFoundException $e) {
         $this->respondTo('html', function () {
             $this->getResponse()->redirect('App\\Admin\\Controllers\\ProductController', 'index');
         });
     } catch (\Exception $e) {
         $this->respondTo('html', function () {
             $this->getResponse()->redirect('App\\Admin\\Controllers\\ProductController', 'index');
         });
     }
 }