lithium\data\source\MongoDb::update PHP Method

update() public method

Update document
public update ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean
    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;
        });
    }

Usage Example

Ejemplo n.º 1
0
 public function testUpdateNoConnectionException()
 {
     $db = new MongoDb(array('host' => '__invalid__', 'autoConnect' => false));
     $this->assertException('Could not connect to the database.', function () use($db) {
         $db->update(null);
     });
 }
All Usage Examples Of lithium\data\source\MongoDb::update