Skip to content

Commit

Permalink
Merge pull request #2 from phishy/master
Browse files Browse the repository at this point in the history
Added support for Model::find('all') and Model::find('view_name'), with key filters
  • Loading branch information
gwoo committed Jan 26, 2013
2 parents ce86a4a + f8a9d38 commit d6f21d3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
50 changes: 47 additions & 3 deletions extensions/data/source/Couchbase.php
Expand Up @@ -10,6 +10,8 @@


use Couchbase as Couch; use Couchbase as Couch;
use lithium\core\NetworkException; use lithium\core\NetworkException;
use lithium\core\Environment;
use lithium\util\Inflector;


/** /**
* A data source adapter which allows you to connect to the Couchbase database engine. * A data source adapter which allows you to connect to the Couchbase database engine.
Expand Down Expand Up @@ -64,6 +66,13 @@ class Couchbase extends \lithium\data\Source {
*/ */
protected $_autoConfig = array('schema', 'classes' => 'merge'); protected $_autoConfig = array('schema', 'classes' => 'merge');


/**
* List of views indexed by design document
*
* @var array
*/
protected $_views = array();

/** /**
* *
*/ */
Expand Down Expand Up @@ -109,6 +118,17 @@ public static function enabled($feature = null) {
* their respective properties in `Model`. * their respective properties in `Model`.
*/ */
public function configureClass($class) { public function configureClass($class) {
$pieces = explode('\\', $class);
$model = array_pop($pieces);
$source = Inflector::tableize($model);
$prefix = (Environment::get() == 'production') ? '' : 'dev_';
$this->_views[$source] = json_decode($this->getDesignDoc("{$prefix}{$source}"), true);
$this->_views[$source] = $this->_views[$source]['views'];
if ($this->_views[$source]) {
foreach ($this->_views[$source] as $k => $v) {
$class::finder($k, array('conditions' => array('view' => $k)));
}
}
return array('schema' => array(), 'meta' => array('key' => 'id', 'locked' => false)); return array('schema' => array(), 'meta' => array('key' => 'id', 'locked' => false));
} }


Expand Down Expand Up @@ -231,16 +251,40 @@ public function read($query, array $options = array()) {
)))); ))));
$key = $model::key(); $key = $model::key();


if (empty($conditions[$key])) { $viewName = '';
return null; $prefix = (Environment::get() == 'production') ? '' : 'dev_';

if (!$conditions) {
$viewName = 'all';
} }

if (!empty($conditions['view'])) {
$viewName = $conditions['view'];
}

$viewOptions = array('stale' => false);
if (isset($conditions['key'])) {
$viewOptions['key'] = $conditions['key'];
}

if ($viewName) {
$view = $self->connection->view($prefix . $source, $viewName, $viewOptions);
$records = array();
if (!empty($view['rows'])) {
foreach ($view['rows'] as $r) {
$records[$r['id']] = $r['value'];
}
}
return $self->item($model, $records, array('class' => 'set', 'exists' => true));
}

$conditions += array('callback' => null, 'cas' => null); $conditions += array('callback' => null, 'cas' => null);
$key = "{$source}:{$conditions[$key]}"; $key = "{$source}:{$conditions[$key]}";
$data = $self->connection->get($key, $conditions['callback'], $conditions['cas']); $data = $self->connection->get($key, $conditions['callback'], $conditions['cas']);


if ($result = json_decode($data, true)) { if ($result = json_decode($data, true)) {
$config = compact('query') + array('exists' => true); $config = compact('query') + array('exists' => true);
return $this->item($model, array('data' => $result), $config); return $self->item($model, array('data' => $result), $config);
} }
return false; return false;
}); });
Expand Down
55 changes: 55 additions & 0 deletions tests/integration/data/CrudExtendedTest.php
Expand Up @@ -52,6 +52,25 @@ public function skip() {
* Creating the test database * Creating the test database
*/ */
public function setUp() { public function setUp() {
$views = array(
'views' =>
array(
'all' =>
array(
'map' =>
"function (doc, meta) { if(doc._source == 'companies') { emit(meta.id,
doc) }}",
),
'by_active' =>
array(
'map' =>
"function (doc, meta) { if(doc._source == 'companies') { emit(doc.active, doc
) }}",
),
),
);
$result = $this->db->setDesignDoc('dev_companies', json_encode($views));
$this->assertTrue($result);
//$this->db->connection->put($this->_database); //$this->db->connection->put($this->_database);
} }


Expand Down Expand Up @@ -108,4 +127,40 @@ public function testCreateIdCustomKey() {
$this->assertEqual('something', $custom->my_key); $this->assertEqual('something', $custom->my_key);
$this->assertTrue($custom->delete()); $this->assertTrue($custom->delete());
} }

public function testFindAll() {
$company = Companies::create($this->data[0]);
$this->assertTrue($company->save());
$companies = Companies::find('all');
$data = $companies->data();
$this->assertEqual(1, count($data));
$record = array_shift($data);
$this->assertEqual('Marine Store', $record['name']);
$this->assertEqual('1', $record['active']);
$this->assertEqual('companies', $record['_source']);
$this->assertTrue($company->delete());
}

public function testFindByView() {
$company1 = Companies::create($this->data[0]);
$company1->save();
$company2 = Companies::create($this->data[1]);
$company2->save();

$company = Companies::find($company1->id);
$data = $company->data();
$this->assertEqual('Marine Store', $data['name']);

$companies = Companies::find('by_active');
$this->assertEqual(2, count($companies->data()));

$companies = Companies::find('all', array('conditions' => array('view' => 'by_active')));
$this->assertEqual(2, count($companies->data()));

$companies = Companies::find('by_active', array('conditions' => array('key' => false)));
$this->assertEqual(1, count($companies->data()));

$company1->delete();
$company2->delete();
}
} }

0 comments on commit d6f21d3

Please sign in to comment.