示例#1
0
 public function getNext()
 {
     $next = $this->cursor->getNext();
     if ($next !== null) {
         $next = Artera_Mongo::createDocument($next, $this->collection->getName());
     }
     return $next;
 }
示例#2
0
 public function __call($name, $arguments)
 {
     if (in_array($name, array('listCollections'))) {
         Artera_Mongo::checkConnection();
     }
     if (method_exists($this->db, $name)) {
         return Artera_Mongo::bind($this, call_user_func_array(array($this->db, $name), $arguments));
     }
 }
示例#3
0
 public function getDBRef($ref)
 {
     $data = $this->collection->getDBRef($ref);
     if ($data === null) {
         return null;
     }
     $doc = Artera_Mongo::createDocument($data, $ref['$ref']);
     $doc->setReference($ref);
     return $doc;
 }
示例#4
0
 protected function translate($value, $originalData = false)
 {
     return Artera_Mongo::documentOrSet($value, "{$this->parentPath}.\$", $this, $originalData);
 }
示例#5
0
 /**
  * Save the document to the mapped collection.
  */
 public function save()
 {
     if (!$this->isReference() && $this->parent() !== null && !array_key_exists('_id', $this->_data)) {
         $root = $this;
         while ($root->parent() !== null) {
             $root = $root->parent();
         }
         if ($root instanceof Artera_Mongo_Document_Set) {
             throw new Artera_Mongo_Exception('Invalid Document_Set. A Document_Set must have a parent.');
         }
         return $root->save();
     }
     Artera_Mongo::checkConnection();
     $isInsert = !isset($this->_id);
     $this->fireEvent('pre-save', array($this));
     $this->fireEvent('pre-' . ($isInsert ? 'insert' : 'update'), array($this));
     $data = $this->data(false);
     if ($isInsert) {
         $insdata = $this->data();
         $insdata['_class'] = get_class($this);
         $this->collection()->insert($insdata);
         $data['_id'] = $insdata['_id'];
     } else {
         $update = array();
         if (count($this->_newdata)) {
             $update['$set'] = array();
             foreach ($this->_newdata as $field => $newdata) {
                 if ($newdata instanceof Artera_Mongo_Document_Set || $newdata instanceof Artera_Mongo_Document) {
                     $newdata = $newdata->savedata();
                 }
                 $update['$set'][$field] = $newdata;
             }
         }
         foreach ($data as $field => $olddata) {
             if (!isset($update['$set'][$field])) {
                 if ($olddata instanceof Artera_Mongo_Document_Set) {
                     $olddata = $olddata->savedata();
                     if ($olddata !== null) {
                         $update['$set'][$field] = $olddata;
                     }
                 } elseif ($olddata instanceof Artera_Mongo_Document && $olddata->modified() && !$olddata->isReference()) {
                     $update['$set'][$field] = $olddata->savedata();
                 }
             }
         }
         if (count($this->_unsetdata)) {
             $update['$unset'] = array();
             foreach ($this->_unsetdata as $name) {
                 $update['$unset'][$name] = 1;
             }
         }
         if (!empty($update)) {
             $this->collection()->update(array('_id' => $this->_id), $update);
         }
     }
     $this->_data = $data;
     $this->_newdata = array();
     $this->_unsetdata = array();
     $this->fireEvent('post-save', array($this));
     $this->fireEvent('post-' . ($isInsert ? 'insert' : 'update'), array($this));
     return $this;
 }
示例#6
0
 /**
  * Sets the default Artera_Mongo_Document class used if no mapping is specified
  * @param string $documentClass A subclass of Artera_Mongo_Document
  */
 public static function setDefaultDocumentClass($documentClass)
 {
     if (!is_subclass_of($documentClass, 'Artera_Mongo_Document')) {
         throw new Artera_Mongo_Exception('$documentClass must be a subclass of Artera_Mongo_Document');
     }
     self::$_defaultDocumentClass = $documentClass;
 }