/** * If `$vals` is false, we're creating a new object from scratch. * If it contains an array, it's a new object from an array. * If `$is_new` is false, then the array is an existing field * (mainly used internally by `fetch()`). * If `$vals` contains a single value, the object is retrieved from the database. */ public function __construct($vals = false, $is_new = true) { $this->name = $this->name === '' ? strtolower(get_class($this)) : $this->name; $this->db = MongoManager::get_database(); if ($this->db) { $this->collection = $this->db->{$this->name}; } if (is_object($vals)) { if (get_class($vals) !== 'MongoId') { $vals = (array) $vals; } } if (is_array($vals)) { $this->data = $vals; if (isset($vals[$this->key])) { $this->keyval = $vals[$this->key]; } if ($is_new) { $this->is_new = true; } } elseif ($vals !== false) { $res = $this->collection->findOne(array('_id' => $this->_id($vals))); if (!$res) { $this->error = 'No object by that ID.'; } else { $this->data = (array) $res; $this->keyval = $this->data[$this->key]; } } else { $this->is_new = true; } }