/** * Save a newly created Emoji to database * * @param Emoji $emoji * @return bool */ public function save(Emoji $emoji) { //construct a sql statement $sql = "INSERT INTO emojis (emojiname, emojichar, keywords, category, created_at, updated_at, created_by) VALUES(?,?,?,?,?,?,?)"; //get a database connection $connection = $this->getConnection(); //prepare a statement $statement = $connection->prepare($sql); /* * I have to get the object properties here because, apparently * PDOStatement::bindParam() does not allow passing of objects by reference, sad * stuff. */ $name = $emoji->getName(); $char = $emoji->getChar(); $keywords = $emoji->getKeywords(); $category = $emoji->getCategory(); $createdAt = $emoji->getCreatedAt(); $updatedAt = $emoji->getUpdatedAt(); $createdBy = $emoji->getCreatedBy(); //bind params $statement->bindParam(1, $name); $statement->bindParam(2, $char); $statement->bindParam(3, $keywords); $statement->bindParam(4, $category); $statement->bindParam(5, $createdAt); $statement->bindParam(6, $updatedAt); $statement->bindParam(7, $createdBy); //execute statement $statement->execute(); //check to see if record has been saved, if it isn't //throw an exception if ($statement->rowCount() > 0) { return true; } throw new PDOException("Error: Unable to save emoji"); }
/** * @param Slim $app * @param $id * @return mixed */ public static function patch(Slim $app, $id) { $response = static::getContentType($app); $name = $app->request->params('emojiname'); $char = $app->request->params('emojichar'); $category = $app->request->params('category'); $updatedAt = date('Y-m-d H:i:s'); $keywords = $app->request->params('keywords'); $emoji = new Emoji($name, $char, $keywords, $category); $emoji->setUpdatedAt($updatedAt); $manager = new EmojiManager(); try { $result = $manager->update($id, $emoji); if ($result) { $response->body(json_encode(['status' => 200, 'message' => 'Record modified'])); return $response; } $response->body(json_encode(['status' => '500', 'message' => 'An error occured while fulfilling request.'])); return $response; } catch (PDOException $e) { $response->body(json_encode(['status' => 304, 'message' => $e->getMessage()])); return $response; } }