Example #1
0
    public function create($title, $content, $image, $idAuthor)
    {
        $article = new Article($this->db);
        try {
            $article->setTitle($title);
            $article->setContent($content);
            $article->setImage($image);
            $article->setIdAuthor($idAuthor);
        } catch (Exception $e) {
            $errors = $e->getMessage();
            echo $errors;
        }
        if (!isset($err)) {
            $title = $this->db->quote($article->getTitle());
            $content = $this->db->quote($article->getContent());
            $idAuthor = $article->getIdAuthor();
            if ($image == "") {
                $query = 'INSERT INTO article(title, content, idAuthor)
						VALUE (' . $title . ',' . $content . ',' . $idAuthor . ')';
            } else {
                $image = $this->db->quote($article->getImage());
                $query = 'INSERT INTO article(title, content, image, idAuthor)
						VALUE (' . $title . ',' . $content . ',' . $image . ',' . $idAuthor . ')';
            }
        }
        $res = $this->db->exec($query);
        if ($res) {
            $id = $this->db->lastInsertId();
            if ($id) {
                return $this->findById($id);
            } else {
                throw new Exception('Database error');
            }
        } else {
            throw new Exception($errors);
        }
    }