Skip to content

Instantly share code, notes, and snippets.

@hndr91
Last active December 9, 2015 09:48
Show Gist options
  • Save hndr91/137f016db8d7ef43c912 to your computer and use it in GitHub Desktop.
Save hndr91/137f016db8d7ef43c912 to your computer and use it in GitHub Desktop.
slim mongo dbHandler
<?php
class dbHandler {
private $con;
private $col;
function __construct() {
require_once dirname(__FILE__) . '/dbConnect.php';
$db = new dbConnect();
//Connect to database
$this->con = $db->connect();
//Select "friends" collection, already defined in config.php
$this->col = new MongoCollection($this->con, DB_COLLECTION);
}
//Get All Friends
public function getAllFriends() {
//Find All friend in friends collection
$cur = $this->col->find();
return $cur;
}
/**
* Insert a New friend
* params $name, $age
*/
public function insertFriend($name, $age) {
//Create array document
$document = array(
"name" => $name,
"age" => $age
);
//Insert to collection
try {
$cur = $this->col->insert($document);
return INSERT_COL_SUCCESS;
}
catch (MongoCursorException $e) {
return INSERT_COL_FAILED;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment