public function addSubcategory(Subcategory $subcategory) { $subcategoryId = $subcategory->getId(); if ($this->hasSubcategory($subcategoryId)) { throw new \Exception(sprintf('Subcategory %s already exists', $subcategoryId)); } $this->subcategories[$subcategoryId] = $subcategory; }
public function insert(Category $category, Subcategory $subcategory, Brand $brand, Item $item) { // $sql = "INSERT INTO item values (null, 'aalluu', 'asdfasd',true,true, '2015-10-10','asdfasf', 'asdfasd', 1, 1 ,1, 1)"; $sql = "INSERT INTO item values (null, '" . $item->getTitle() . "','" . $item->getDetail() . "',true,false, '" . $item->getDate() . "', '" . $item->getLocation() . "','asfasf'," . $category->getId() . "," . $subcategory->getId() . ", 1 ," . $brand->getId() . ")"; echo $sql; if ($this->conn->query($sql) === TRUE) { echo "Insert operation successful"; } else { echo "Error inserting: "; } }
/** * @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; } }
/** * @param Subcategory $subcategory * @return bool */ public function setSubcategory(Subcategory $subcategory) { $this->subcategory = $subcategory; $this->id_subcategory = $subcategory->getId(); return true; }