Пример #1
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));
     }
 }
Пример #2
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;
 }