예제 #1
0
 /**
  * @param $name
  * @param $description
  * @param array $errors
  * @return object
  * @throws Exception
  */
 public function create($name, $description, $errors = array())
 {
     $brand = new Brand($this->db);
     try {
         $brand->setName($name);
     } catch (Exception $e) {
         $errors[] = $e->getMessage();
     }
     try {
         $brand->setDescription($description);
     } catch (Exception $e) {
         $errors[] = $e->getMessage();
     }
     if (count($errors) == 0) {
         $name = $this->db->quote($brand->getName());
         $description = $this->db->quote($brand->getDescription());
         $query = "INSERT INTO brand (name, description) VALUES(" . $name . ", " . $description . ")";
         $data = $this->db->exec($query);
         if ($data) {
             $id = $this->db->lastInsertId();
             if ($id) {
                 try {
                     $brand = $this->findById($id);
                     return $brand;
                 } catch (Exception $e) {
                     $errors[] = $e->getMessage();
                     return $errors;
                 }
             } else {
                 throw new Exception('Last insert error');
             }
         } else {
             throw new Exception('Insert error');
         }
     } else {
         return $errors;
     }
 }