lithium\data\source\MongoDb::update()
Implements
lithium\data\Source::update()
Update document
Parameters
-
string
$query
-
array
$options
Returns
booleanFilter
This method can be filtered.
Source
public function update($query, array $options = array()) {
$_config = $this->_config;
$defaults = array(
'upsert' => false,
'multiple' => true,
'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) {
$options = $params['options'];
$query = $params['query'];
$args = $query->export($self, array('keys' => array('conditions', 'source', 'data')));
$source = $args['source'];
$data = $args['data'];
if ($query->entity()) {
$data = $_exp::get('update', $data);
}
if ($source === "{$_config['gridPrefix']}.files" && isset($data['update']['file'])) {
$args['data']['_id'] = $self->invokeMethod('_saveFile', array($data['update']));
}
$update = $query->entity() ? $_exp::toCommand($data) : $data;
if (empty($update)) {
return true;
}
if ($options['multiple'] && !preg_grep('/^\$/', array_keys($update))) {
$update = array('$set' => $update);
}
$result = $self->connection->{$source}->update($args['conditions'], $update, $options);
if ($self->invokeMethod('_ok', array($result))) {
$query->entity() ? $query->entity()->sync() : null;
return true;
}
return false;
});
}