Last active
December 9, 2015 09:48
-
-
Save hndr91/137f016db8d7ef43c912 to your computer and use it in GitHub Desktop.
slim mongo dbHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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