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

read() public method

Read from document.
public read ( string $query, array $options = [] ) : object
$query string
$options array
return object
    public function read($query, array $options = array())
    {
        $defaults = array('return' => 'resource', '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) {
            $query = $params['query'];
            $params = $query->export($self);
            list($_path, $conditions) = (array) $params['conditions'];
            $model = $query->model();
            if (empty($_path)) {
                $_path = '_all_docs';
                $conditions['include_docs'] = 'true';
            }
            $path = "{$config['database']}/{$_path}";
            $args = (array) $conditions + (array) $params['limit'] + (array) $params['order'];
            $result = $conn->get($path, $args);
            $result = is_string($result) ? json_decode($result, true) : $result;
            $data = $stats = array();
            if (isset($result['_id'])) {
                $data = array($result);
            } elseif (isset($result['rows'])) {
                $data = $result['rows'];
                unset($result['rows']);
                $stats = $result;
            }
            $stats += array('total_rows' => null, 'offset' => null);
            $opts = compact('stats') + array('class' => 'set', 'exists' => true, 'defaults' => false);
            return $self->item($model, $data, $opts);
        });
    }

Usage Example

Esempio n. 1
0
 public function testReadWithViewConditions()
 {
     $couchdb = new CouchDb($this->_testConfig);
     $this->_query->conditions(array('design' => 'latest', 'view' => 'all', 'limit' => 10, 'descending' => 'true'));
     $result = $couchdb->read($this->_query);
     $this->assertEqual(array('total_rows' => 3, 'offset' => 0), $result->stats());
     $expected = array('id' => 'a1', 'rev' => '1-1', 'author' => 'author 1', 'body' => 'body 1');
     $result = $result->data();
     $this->assertEqual($expected, $result['a1']);
     $expected = '/lithium-test/_design/latest/_view/all/';
     $result = $couchdb->last->request->path;
     $this->assertEqual($expected, $result);
     $expected = '?limit=10&descending=true';
     $result = $couchdb->last->request->queryString();
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\data\source\http\adapter\CouchDb::read