public function create(Category $category, $name, $description, $img) { $sub_category = new SubCategory($this->db); $errors = array(); try { $sub_category->setCategory($category); } catch (Exception $e) { $errors[] = $e->getMessage(); } try { $sub_category->setName($name); } catch (Exception $e) { $errors[] = $e->getMessage(); } try { $sub_category->setDescription($description); } catch (Exception $e) { $errors[] = $e->getMessage(); } try { $sub_category->setImg($img); } catch (Exception $e) { $errors[] = $e->getMessage(); } if (count($errors) == 0) { $idCategory = intval($sub_category->getIdCategory()); $name = $this->db->quote($sub_category->getName()); $description = $this->db->quote($sub_category->getDescription()); $img = $this->db->quote($sub_category->getImg()); $query = "INSERT INTO sub_category (id_category, name, description, img) VALUES ('" . $idCategory . "', " . $name . ", " . $description . ", " . $img . ")"; $res = $this->db->exec($query); if ($res) { $id = $this->db->lastInsertId(); if ($id) { return $this->findById($id); } else { throw new Exception("Internal server error"); } } } else { return $errors; } }
public function create($name, $description, $image, Category $category) { $errors = array(); $subCategory = new SubCategory($this->db); $valide = $subCategory->setDescription($description); if ($valide === true) { $valide = $subCategory->setName($name); if ($valide === true) { $valide = $subCategory->setImage($image); if ($valide === true) { $valide = $subCategory->setCategory($category); if ($valide === true) { $description = $this->db->quote($subCategory->getDescription()); $name = $this->db->quote($subCategory->getName()); $image = $this->db->quote($subCategory->getImage()); $id_category = $subCategory->getCategory()->getId(); $query = "INSERT INTO subcategory (description, name, image, id_category) VALUES (" . $description . ", " . $name . ", " . $image . ", " . $id_category . ")"; $res = $this->db->query($query); if ($res) { $id = $this->db->lastInsertId(); if ($id) { try { return $this->findById($id); } catch (Exception $e) { $errors[] = $e->getMessage(); } } else { throw new Exception("internal server error"); } } } else { return $valide; } } else { return $valide; } } else { return $valide; } } else { return $valide; } }