lithium\data\source\http\adapter\CouchDb::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())
    {
        $params = compact('query', 'options');
        $conn =& $this->connection;
        $config = $this->_config;
        return $this->_filter(__METHOD__, $params, function ($self, $params) use(&$conn, $config) {
            $query = $params['query'];
            $options = $params['options'];
            $params = $query->export($self);
            list($_path, $conditions) = (array) $params['conditions'];
            $data = $query->data();
            foreach (array('id', 'rev') as $key) {
                $data["_{$key}"] = isset($data[$key]) ? (string) $data[$key] : null;
                unset($data[$key]);
            }
            $data = (array) $conditions + array_filter((array) $data);
            $retry = false;
            do {
                $result = $conn->put("{$config['database']}/{$_path}", $data, array('type' => 'json'));
                $result = is_string($result) ? json_decode($result, true) : $result;
                $retry = $retry ? !$retry : $self->invokeMethod('_autoBuild', array($result));
            } while ($retry);
            if (isset($result['_id']) || isset($result['ok']) && $result['ok'] === true) {
                $result = $self->invokeMethod('_format', array($result, $options));
                $query->entity()->sync($result['id'], array('rev' => $result['rev']));
                return true;
            }
            if (isset($result['error'])) {
                $query->entity()->errors(array($result['error']));
            }
            return false;
        });
    }

Usage Example

Beispiel #1
0
 public function testUpdate()
 {
     $couchdb = new CouchDb($this->_testConfig);
     $this->query->data(array('id' => 12345, 'rev' => '1-1', 'title' => 'One'));
     $result = $couchdb->update($this->query);
     $this->assertTrue($result);
     $expected = '/lithium-test/12345';
     $result = $couchdb->last->request->path;
     $this->assertEqual($expected, $result);
     $expected = array();
     $result = $couchdb->last->request->params;
     $this->assertEqual($expected, $result);
 }