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; } }