lithium\data\source\MongoDb::create()
Implements
lithium\data\Source::create()
Create new document
Parameters
-
string
$query
-
array
$options
Returns
booleanFilter
This method can be filtered.
Source
public function create($query, array $options = array()) {
$_config = $this->_config;
$defaults = array(
'w' => $_config['w'],
'wTimeoutMS' => $_config['wTimeoutMS'],
'fsync' => false
);
$options += $defaults;
$this->_checkConnection();
$params = compact('query', 'options');
$_exp = $this->_classes['exporter'];
return $this->_filter(__METHOD__, $params, function($self, $params) use ($_config, $_exp) {
$query = $params['query'];
$options = $params['options'];
$args = $query->export($self, array('keys' => array('source', 'data')));
$data = $_exp::get('create', $args['data']);
$source = $args['source'];
if ($source === "{$_config['gridPrefix']}.files" && isset($data['create']['file'])) {
$result = array('ok' => true);
$data['create']['_id'] = $self->invokeMethod('_saveFile', array($data['create']));
} else {
$result = $self->connection->{$source}->insert($data['create'], $options);
$result = $self->invokeMethod('_ok', array($result));
}
if ($result === true || isset($result['ok']) && (boolean) $result['ok'] === true) {
if ($query->entity()) {
$query->entity()->sync($data['create']['_id']);
}
return true;
}
return false;
});
}