lithium\data\source\http\adapter\CouchDb::create PHP Method

create() public method

Create new document.
public create ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean
    public function create($query, array $options = array())
    {
        $defaults = array('model' => $query->model());
        $options += $defaults;
        $params = compact('query', 'options');
        $conn =& $this->connection;
        $config = $this->_config;
        return $this->_filter(__METHOD__, $params, function ($self, $params) use(&$conn, $config) {
            $request = array('type' => 'json');
            $query = $params['query'];
            $options = $params['options'];
            $data = $query->data();
            $data += array('type' => $options['model']::meta('source'));
            if (isset($data['id'])) {
                return $self->update($query, $options);
            }
            $retry = false;
            do {
                $result = $conn->post($config['database'], $data, $request);
                $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'], $result);
                return true;
            }
            return false;
        });
    }

Usage Example

Ejemplo n.º 1
0
 public function testCreateWithId()
 {
     $couchdb = new CouchDb($this->_testConfig);
     $this->query->data(array('id' => 12345, 'name' => 'Acme Inc.'));
     $expected = true;
     $result = $couchdb->create($this->query);
     $this->assertEqual($expected, $result);
     $expected = '/posts/12345';
     $result = $couchdb->last->request->path;
     $this->assertEqual($expected, $result);
     $expected = array();
     $result = $couchdb->last->request->params;
     $this->assertEqual($expected, $result);
 }