Example #1
0
    public function create(Category $category, $name, $price, $stock, $image, $description)
    {
        $errors = array();
        $item = new Item($this->db);
        try {
            $item->setCategory($category);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $item->setName($name);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $item->setPrice($price);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $item->setStock($stock);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $item->setImage($image);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        try {
            $item->setDescription($description);
        } catch (Exception $e) {
            $errors[] = $e->getMessage();
        }
        if (count($errors) == 0) {
            $name = $this->db->quote($item->getName());
            $price = $this->db->quote($item->getPrice());
            $stock = $this->db->quote($item->getStock());
            $image = $this->db->quote($item->getImage());
            $description = $this->db->quote($item->getDescription());
            $idCategory = $item->getCategory()->getId();
            $query = '	INSERT INTO item (id_category, name, price, stock, image, description)
								VALUES(' . $idCategory . ',' . $name . ',' . $price . ',' . $stock . ',' . $image . ',' . $description . ')';
            $res = $this->db->exec($query);
            if ($res) {
                $id = $this->db->lastInsertId();
                if ($id) {
                    return $this->readByID($id);
                } else {
                    throw new Exception('Internal server Error');
                }
            }
        }
    }
 /**
  * @param Subcategory $subcategory
  * @param $name
  * @param $descr
  * @param $short_descr
  * @param $price
  * @param $stock
  * @return array
  * @throws Exception
  */
 public function create(Subcategory $subcategory, $name, $descr, $short_descr, $price, $stock)
 {
     $errors = array();
     $item = new Item($this->db);
     try {
         $item->setName($name);
         $item->setDescription($descr);
         $item->setShortDescription($short_descr);
         $item->setPrice($price);
         $item->setStock($stock);
     } catch (Exception $e) {
         $errors[] = $e->getMessage();
     }
     if (count($errors) == 0) {
         $name = $this->db->quote($item->getName());
         $description = $this->db->quote($item->getDescription());
         $shortDescription = $this->db->quote($item->getShortDescription());
         $price = $this->db->quote($item->getPrice());
         $stock = $this->db->quote($item->getStock());
         $query = "  INSERT INTO item(id_subcategory, name, descr, short_descr, price, stock)\n                               VALUES(" . $subcategory->getId() . ", " . $name . ", " . $description . ", " . $shortDescription . ", " . $price . ", " . $stock . ")";
         $data = $this->db->exec($query);
         if ($data) {
             $id = $this->db->lastInsertId();
             if ($id) {
                 try {
                     return $this->findById($id);
                 } catch (Exception $e) {
                     $errors[] = $e->getMessage();
                     return $errors;
                 }
             } else {
                 throw new Exception('Last insert error');
             }
         } else {
             throw new Exception('Db error');
         }
     } else {
         return $errors;
     }
 }