예제 #1
0
 /**
  * Document constructor.
  * 
  * @param null $id The id for a pre-existing document
  * @param null $fields Fields to use to populate the properties of the document.
  */
 public function __construct($id = null, $fields = null)
 {
     try {
         $this->doc_db = DocumentStore::Get($this->doc_database);
     } catch (Exception $ex) {
     }
     $props = get_object_vars($this);
     $props['_id'] = $id ? $id : uuid();
     foreach ($props as $key => $prop) {
         if (strpos($key, 'doc_') === FALSE) {
             if (is_array($prop)) {
                 $this->doc_fields[$key] = new ArrayObject();
             } else {
                 $this->doc_fields[$key] = $prop;
             }
             unset($this->{$key});
         }
     }
     if ($id != null) {
         if (is_numeric($id)) {
             $id = (int) $id;
         }
         $res = $this->doc_db->query($this, array('_id' => $id), null, 0, 0, false);
         if (count($res) == 0) {
             throw new DocumentNotFoundException("Invalid document identifier: {$id}");
         }
         $fields = $res[0];
         $this->id = $id;
         $this->doc_state = Document::DOCUMENT_LIVE;
     }
     if ($fields != null) {
         foreach ($fields as $key => $val) {
             if (array_key_exists($key, $this->doc_fields)) {
                 if ((is_array($val) || $val instanceof Traversable) && array_key_exists('__class', $val)) {
                     $class = $val['__class'];
                     if (class_exists($class)) {
                         $val = new $class(null, $val);
                     }
                 } else {
                     if (is_string($val)) {
                         $val = utf8_decode($val);
                     }
                 }
                 $this->doc_fields[$key] = $val;
             }
         }
         $this->doc_state = Document::DOCUMENT_LIVE;
     }
 }