Created
June 14, 2011 21:29
-
-
Save tobsn/1025942 to your computer and use it in GitHub Desktop.
mini mongodb class
This file contains 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
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