Ejemplo n.º 1
0
 /**
  * Returns the database object (the connection)
  *
  * @return MongoDB
  */
 public static function db()
 {
     if (isset(static::$database) && static::$database instanceof MongoDB) {
         return static::$database;
     }
     if (is_null(static::$client)) {
         return Client::db(static::$database);
     }
     return static::client()->selectDB(static::$database);
 }
Ejemplo n.º 2
0
 /**
  * @depends testMakeConnection
  */
 public function testGetDB($conn)
 {
     $db = Client::db('test');
     $this->assertTrue($db instanceof MongoDB);
     $database = $conn->getDefaultDatabase();
     $this->assertEquals('test', $database);
     $conn->setDefaultDatabase('test2');
     $database = $conn->getDefaultDatabase();
     $this->assertEquals('test2', $database);
     $this->assertTrue(Model::db() instanceof MongoDB);
     $conn->setDefaultDatabase('test');
 }
Ejemplo n.º 3
0
 /**
  * Find documents from the collection within the criteria
  *
  * @param Array $criteria
  * @param Array $projection
  *
  * @return Bool|Irto\NeoMongo\OdmCursor
  */
 public static function where(array $criteria, array $projection)
 {
     if ($criteria instanceof MongoId || Client::isMongoId($criteria)) {
         $criteria = ['_id' => $criteria];
     }
     $criteria = static::prepareMongoAttributes($criteria);
     if (is_array($criteria)) {
         $projection = static::prepareProjection($projection);
         $instance = new static();
         if (method_exists($instance, 'setup')) {
             $instance->setup();
         }
         $cursor = new OdmCursor(static::collection()->find($criteria, $projection), $instance);
         return $cursor;
     }
     return false;
 }
Ejemplo n.º 4
0
 /**
  * Prepare attributes to be used in MongoDb.
  * especially the _id.
  *
  * @param array $attr
  * @return array
  */
 protected static function prepareMongoAttributes($attr)
 {
     // Translate the primary key field into _id
     if (isset($attr['_id'])) {
         // If its a 24 digits hexadecimal, then it's a MongoId
         if (Client::isMongoId($attr['_id'])) {
             $attr['_id'] = new MongoId($attr['_id']);
         } elseif (is_numeric($attr['_id'])) {
             $attr['_id'] = (int) $attr['_id'];
         } else {
             $attr['_id'] = $attr['_id'];
         }
     }
     return $attr;
 }