lithium\data\source\http\adapter\CouchDb::read()
Implements
lithium\data\Source::read()
Read from document.
Parameters
-
string
$query
-
array
$options
Returns
objectFilter
This method can be filtered.
Source
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);
});
}