/** * {@inheritdoc} */ public function add(Article $article) { $id = $article->getIdentifier(); $params = [':title' => $article->getTitle(), ':body' => $article->getBody(), ':year' => $article->getYear()]; $bind = [':year' => \PDO::PARAM_INT]; if ($id) { $sql = 'UPDATE ' . self::TABLE . ' SET title = :title, body = :body, publish_year = :year WHERE id = :id'; $params[':id'] = $id; $bind[':id'] = \PDO::PARAM_INT; } else { $sql = 'INSERT INTO ' . self::TABLE . ' (title, body, publish_year) VALUES (:title, :body, :year)'; } $stm = $this->conn->prepare($sql); foreach ($params as $name => $val) { $stm->bindValue($name, $val, isset($bind[$name]) ? $bind[$name] : \PDO::PARAM_STR); } $stm->execute(); return $id ? $id : intval($this->conn->lastInsertId()); }