public function __construct($data = array(), $config = array())
{
// Make sure mongo is initialised
Shanty_Mongo::init();
$this->_config = array_merge($this->_config, $config);
$this->_references = new SplObjectStorage();
// If not connected and this is a new root document, figure out the db and collection
if ($this->isNewDocument() && $this->isRootDocument() && !$this->isConnected()) {
$this->setConfigAttribute('connectionGroup', static::getConnectionGroupName());
$this->setConfigAttribute('db', static::getDbName());
$this->setConfigAttribute('collection', static::getCollectionName());
}
// Get collection requirements
$this->_docRequirements = static::getCollectionRequirements();
// apply requirements requirement modifiers
$this->applyRequirements($this->_config['requirementModifiers'], false);
// Store data
$this->_cleanData = $data;
// Initialize input data
if ($this->isNewDocument() && is_array($data)) {
foreach ($data as $key => $value) {
$this->getProperty($key);
}
}
// Create document id if one is required
if ($this->isNewDocument() && ($this->hasKey() || isset($this->_config['hasId']) && $this->_config['hasId'])) {
$this->_data['_id'] = new MongoId();
$this->_data['_type'] = static::getCollectionInheritance();
}
// If has key then add it to the update criteria
if ($this->hasKey()) {
$this->setCriteria($this->getPathToProperty('_id'), $this->getId());
}
$this->init();
}