Example #1
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;
 }
Example #2
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;
 }