Skip to content

Instantly share code, notes, and snippets.

@tobsn
Created June 14, 2011 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobsn/1025942 to your computer and use it in GitHub Desktop.
Save tobsn/1025942 to your computer and use it in GitHub Desktop.
mini mongodb class
class mon {
public $mongoobj = null;
public $db;
function __construct( $db ) {
$this->db = $db;
return;
}
function con() {
if( $this->mongoobj === null ) {
$this->mongoobj = new Mongo();
}
return $this->mongoobj;
}
function command( $data ) {
if( $this->mongoobj === null ) {
$this->con();
}
return $this->mongoobj->{$this->db}->command( $data );
}
function __call( $col, $args ) {
if( $this->mongoobj === null ) {
$this->con();
}
$func = $args[0];
unset( $args[0] );
if( $func == 'update' ) {
$data = $this->mongoobj->{$this->db}->$col->$func( $args[1], $args[2] );
}
else {
$data = call_user_func_array( array( $this->mongoobj->{$this->db}->$col, $func ), $args );
}
$result = null;
if( is_object( $data ) && $data->count() > 0 ) {
$result = array();
foreach( $data as $row ) {
$result[] = $row;
}
}
elseif( is_array( $data ) ) {
$result = $data;
}
elseif( $func == 'insert' || $func == 'update' ) {
$result = ( count( $args ) > 1 ) ? $args : $args[0];
}
return $result;
}
}
$mon = new mon( 'databasename' );
$data = $mon->collectionname( 'find', array( 'name' => 'john' ) );
if( $data !== null ) {
print_r( $data );
}
// OR
if( ( $data = $mon->collectionname( 'find', array( 'name' => 'john' ) ) ) !== null ) {
print_r( $data );
}
// --
$mon->collection( 'update', array( 'name' => 'john' ), array( '$set' => array( 'name' => 'Johnason' ) ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment