示例#1
0
 /**
  * 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");
 }