Beispiel #1
0
 public static function findOne($query = [], array $fields = [], $as_array = false)
 {
     $pk = static::getPrimaryKey();
     $query = Criteria::normalize($query, $pk);
     $gridFSFile = static::getCollection()->findOne($query, $fields);
     $model = static::instantiate($gridFSFile->file);
     $model->gridFSFile = $gridFSFile;
     return $model;
 }
Beispiel #2
0
 public function __construct($criteria = null, $hydrate_class, $connection_name = null)
 {
     if (empty($hydrate_class)) {
         throw new Exception('Invalid parameter type.');
     }
     $this->_hydrate_class = $hydrate_class;
     $this->_connection_name = $connection_name;
     parent::__construct($criteria);
 }
Beispiel #3
0
 /**
  * @param mixed $query
  * @param array $fields
  * @param bool  $as_array
  * @return null|self
  */
 public function findOne($query = null, array $fields = [], $as_array = false)
 {
     $class = $this->hydrate_class;
     $pk = $class::getPrimaryKey();
     $query = Criteria::normalize($query, $pk);
     // try to use identity map
     if (count($query) === 1 && array_key_exists($pk, $query)) {
         $id = $query[$pk];
         if ($model = $this->getIdentityMap($id)) {
             if ($as_array) {
                 return $model->getAttributes();
             } else {
                 return $model;
             }
         }
     }
     $this->_connection->logging();
     if ($document = $this->_mongoCollection->findOne($query, $fields)) {
         if ($as_array) {
             return $document;
         }
         /** @var DocumentInterface $model */
         $model = $class::instantiate($document);
         $model->setIsNew(false);
         $model->afterFind($document);
         return $model;
     }
     return null;
 }