Commit 39ff11a3 by Carsten Brandt

first draft of elasticsearch AR

parent 9f121853
...@@ -58,31 +58,21 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -58,31 +58,21 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/ */
public function createCommand($db = null) public function createCommand($db = null)
{ {
/** @var $modelClass ActiveRecord */ /** @var ActiveRecord $modelClass */
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
if ($db === null) { if ($db === null) {
$db = $modelClass::getDb(); $db = $modelClass::getDb();
} }
$index = $modelClass::indexName(); if ($this->type === null) {
$type = $modelClass::indexType(); $this->type = $modelClass::type();
if (is_array($this->where) && Activerecord::isPrimaryKey(array_keys($this->where))) {
// TODO what about mixed queries?
$query = array();
foreach((array) reset($this->where) as $pk) {
$doc = array(
'_id' => $pk,
);
$db->getQueryBuilder()->buildSelect($doc, $this->select);
$query['docs'][] = $doc;
} }
$command = $db->createCommand($query, $index, $type); if ($this->index === null) {
$command->api = '_mget'; $this->index = $modelClass::index();
return $command; $this->type = $modelClass::type();
} else {
$query = $db->getQueryBuilder()->build($this);
return $db->createCommand($query, $index, $type);
} }
$query = $db->getQueryBuilder()->build($this);
return $db->createCommand($query, $this->index, $this->type);
} }
/** /**
...@@ -94,16 +84,15 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -94,16 +84,15 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public function all($db = null) public function all($db = null)
{ {
$command = $this->createCommand($db); $command = $this->createCommand($db);
$rows = $command->queryAll(); $result = $command->queryAll();
if (!empty($rows)) { if ($result['total'] == 0) {
$models = $this->createModels($rows); return [];
}
$models = $this->createModels($result['hits']);
if (!empty($this->with)) { if (!empty($this->with)) {
$this->populateRelations($models, $this->with); $this->findWith($this->with, $models);
} }
return $models; return $models;
} else {
return array();
}
} }
/** /**
...@@ -117,23 +106,22 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -117,23 +106,22 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public function one($db = null) public function one($db = null)
{ {
$command = $this->createCommand($db); $command = $this->createCommand($db);
$row = $command->queryOne(); $result = $command->queryOne();
if ($row !== false) { if ($result['total'] == 0) {
return null;
}
if ($this->asArray) { if ($this->asArray) {
$model = $row; $model = reset($result['hits']);
} else { } else {
/** @var $class ActiveRecord */ /** @var ActiveRecord $class */
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::create(reset($result['hits']));
} }
if (!empty($this->with)) { if (!empty($this->with)) {
$models = array($model); $models = [$model];
$this->populateRelations($models, $this->with); $this->findWith($this->with, $models);
$model = $models[0]; $model = $models[0];
} }
return $model; return $model;
} else {
return null;
}
} }
} }
...@@ -7,6 +7,7 @@ namespace yii\elasticsearch; ...@@ -7,6 +7,7 @@ namespace yii\elasticsearch;
use yii\base\Component; use yii\base\Component;
use yii\db\Exception;
use yii\helpers\Json; use yii\helpers\Json;
// camelCase vs. _ // camelCase vs. _
...@@ -25,9 +26,6 @@ class Command extends Component ...@@ -25,9 +26,6 @@ class Command extends Component
* @var Connection * @var Connection
*/ */
public $db; public $db;
public $api = '_search';
/** /**
* @var string|array the indexes to execute the query on. Defaults to null meaning all indexes * @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html#search-multi-index * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html#search-multi-index
...@@ -43,90 +41,35 @@ class Command extends Component ...@@ -43,90 +41,35 @@ class Command extends Component
*/ */
public $query; public $query;
// private function createUrl($endPoint = null) public function queryAll($options = [])
// { {
// if ($endPoint === null) { $query = $this->query;
// $endPoint = $this->api; if (empty($query)) {
// } $query = '{}';
// if ($this->index === null && $this->type === null) { }
// return '/' . $endPoint; if (is_array($query)) {
// } $query = Json::encode($query);
// $index = $this->index; }
// if ($index === null) { $url = [
// $index = '_all'; $this->index !== null ? $this->index : '_all',
// } elseif (is_array($index)) { $this->type !== null ? $this->type : '_all',
// $index = implode(',', $index); '_search'
// } ];
// $type = $this->type; $response = $this->db->http()->post($this->createUrl($url, $options), null, $query)->send();
// if (is_array($type)) { return Json::decode($response->getBody(true))['hits'];
// $type = implode(',', $type); }
// }
// return '/' . $index . '/' . (empty($type) ? '' : $type . '/') . $endPoint; public function queryOne($options = [])
// } {
// $options['size'] = 1;
// public function queryAll() return $this->queryAll($options);
// { }
// $query = $this->query;
// if (empty($query)) { public function queryCount($options = [])
// $query = '{}'; {
// } $options['search_type'] = 'count';
// if (is_array($query)) { return $this->queryAll($options);
// $query = Json::encode($query); }
// }
// $http = $this->db->http();
// $response = $http->post($this->createUrl(), null, $query)->send();
// $data = Json::decode($response->getBody(true));
// // TODO store query meta data for later use
// $docs = array();
// switch ($this->api) {
// default:
// case '_search':
// if (isset($data['hits']['hits'])) {
// $docs = $data['hits']['hits'];
// }
// break;
// case '_mget':
// if (isset($data['docs'])) {
// $docs = $data['docs'];
// }
// break;
// }
// $rows = array();
// foreach($docs as $doc) {
// // TODO maybe return type info
// if (isset($doc['exists']) && !$doc['exists']) {
// continue;
// }
// $row = $doc['_source'];
// $row['id'] = $doc['_id'];
// $rows[] = $row;
// }
// return $rows;
// }
//
// public function queryOne()
// {
// // TODO set limit
// $rows = $this->queryAll();
// return reset($rows);
// }
//
// public function queryCount()
// {
// //http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html
// $query = $this->query;
// if (empty($query)) {
// $query = '';
// }
// if (is_array($query)) {
// $query = Json::encode($query);
// }
// $http = $this->db->http();
// $response = $http->post($this->createUrl('_count'), null, $query)->send();
// $data = Json::decode($response->getBody(true));
// // TODO store query meta data for later use
// return $data['count'];
// }
/** /**
...@@ -161,8 +104,15 @@ class Command extends Component ...@@ -161,8 +104,15 @@ class Command extends Component
*/ */
public function get($index, $type, $id, $options = []) public function get($index, $type, $id, $options = [])
{ {
$response = $this->db->http()->post($this->createUrl([$index, $type, $id], $options))->send(); $httpOptions = [
'exceptions' => false,
];
$response = $this->db->http()->get($this->createUrl([$index, $type, $id], $options), null, $httpOptions)->send();
if ($response->getStatusCode() == 200 || $response->getStatusCode() == 404) {
return Json::decode($response->getBody(true)); return Json::decode($response->getBody(true));
} else {
throw new Exception('Elasticsearch request failed.');
}
} }
/** /**
......
...@@ -298,7 +298,7 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -298,7 +298,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/ */
public static function getTableSchema() public static function getTableSchema()
{ {
throw new NotSupportedException('getTableSchema() is not supported by redis ActiveRecord'); throw new NotSupportedException('getTableSchema() is not supported by redis ActiveRecord.');
} }
/** /**
...@@ -306,7 +306,7 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -306,7 +306,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/ */
public static function findBySql($sql, $params = []) public static function findBySql($sql, $params = [])
{ {
throw new NotSupportedException('findBySql() is not supported by redis ActiveRecord'); throw new NotSupportedException('findBySql() is not supported by redis ActiveRecord.');
} }
/** /**
......
...@@ -17,15 +17,9 @@ class Customer extends ActiveRecord ...@@ -17,15 +17,9 @@ class Customer extends ActiveRecord
public $status2; public $status2;
public static function columns() public static function attributes()
{ {
return array( return ['name', 'email', 'address', 'status'];
'id' => 'integer',
'name' => 'string',
'email' => 'string',
'address' => 'string',
'status' => 'integer',
);
} }
public function getOrders() public function getOrders()
......
...@@ -11,12 +11,8 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -11,12 +11,8 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class Item extends ActiveRecord class Item extends ActiveRecord
{ {
public static function columns() public static function attributes()
{ {
return array( return ['name', 'category_id'];
'id' => 'integer',
'name' => 'string',
'category_id' => 'integer',
);
} }
} }
...@@ -12,29 +12,24 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -12,29 +12,24 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class Order extends ActiveRecord class Order extends ActiveRecord
{ {
public static function columns() public static function attributes()
{ {
return array( return ['customer_id', 'create_time', 'total'];
'id' => 'integer',
'customer_id' => 'integer',
'create_time' => 'integer',
'total' => 'integer',
);
} }
public function getCustomer() public function getCustomer()
{ {
return $this->hasOne('Customer', array('id' => 'customer_id')); return $this->hasOne('Customer', ['id' => 'customer_id']);
} }
public function getOrderItems() public function getOrderItems()
{ {
return $this->hasMany('OrderItem', array('order_id' => 'id')); return $this->hasMany('OrderItem', ['order_id' => 'id']);
} }
public function getItems() public function getItems()
{ {
return $this->hasMany('Item', array('id' => 'item_id')) return $this->hasMany('Item', ['id' => 'item_id'])
->via('orderItems', function ($q) { ->via('orderItems', function ($q) {
// additional query configuration // additional query configuration
})->orderBy('id'); })->orderBy('id');
...@@ -42,9 +37,9 @@ class Order extends ActiveRecord ...@@ -42,9 +37,9 @@ class Order extends ActiveRecord
public function getBooks() public function getBooks()
{ {
return $this->hasMany('Item', array('id' => 'item_id')) return $this->hasMany('Item', ['id' => 'item_id'])
->viaTable('tbl_order_item', array('order_id' => 'id')) ->viaTable('tbl_order_item', ['order_id' => 'id'])
->where(array('category_id' => 1)); ->where(['category_id' => 1]);
} }
public function beforeSave($insert) public function beforeSave($insert)
......
...@@ -12,23 +12,18 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -12,23 +12,18 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class OrderItem extends ActiveRecord class OrderItem extends ActiveRecord
{ {
public static function columns() public static function attributes()
{ {
return array( return ['order_id', 'item_id', 'quantity', 'subtotal'];
'order_id' => 'integer',
'item_id' => 'integer',
'quantity' => 'integer',
'subtotal' => 'integer',
);
} }
public function getOrder() public function getOrder()
{ {
return $this->hasOne('Order', array('id' => 'order_id')); return $this->hasOne('Order', ['id' => 'order_id']);
} }
public function getItem() public function getItem()
{ {
return $this->hasOne('Item', array('id' => 'item_id')); return $this->hasOne('Item', ['id' => 'item_id']);
} }
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace yiiunit\framework\elasticsearch; namespace yiiunit\framework\elasticsearch;
use yii\db\Query;
use yii\elasticsearch\Connection; use yii\elasticsearch\Connection;
use yii\elasticsearch\ActiveQuery; use yii\elasticsearch\ActiveQuery;
use yii\helpers\Json; use yii\helpers\Json;
...@@ -25,42 +24,53 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -25,42 +24,53 @@ class ActiveRecordTest extends ElasticSearchTestCase
$db->http()->delete('_all')->send(); $db->http()->delete('_all')->send();
$customer = new Customer(); $customer = new Customer();
$customer->setAttributes(array('id' => 1, 'email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1), false); $customer->primaryKey = 1;
$customer->setAttributes(array('email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1), false);
$customer->save(false); $customer->save(false);
$customer = new Customer(); $customer = new Customer();
$customer->setAttributes(array('id' => 2, 'email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1), false); $customer->primaryKey = 2;
$customer->setAttributes(array('email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1), false);
$customer->save(false); $customer->save(false);
$customer = new Customer(); $customer = new Customer();
$customer->setAttributes(array('id' => 3, 'email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2), false); $customer->primaryKey = 3;
$customer->setAttributes(array('email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2), false);
$customer->save(false); $customer->save(false);
// INSERT INTO tbl_category (name) VALUES ('Books'); // INSERT INTO tbl_category (name) VALUES ('Books');
// INSERT INTO tbl_category (name) VALUES ('Movies'); // INSERT INTO tbl_category (name) VALUES ('Movies');
$item = new Item(); $item = new Item();
$item->setAttributes(array('id' => 1, 'name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1), false); $item->primaryKey = 1;
$item->setAttributes(array('name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1), false);
$item->save(false); $item->save(false);
$item = new Item(); $item = new Item();
$item->setAttributes(array('id' => 2, 'name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1), false); $item->primaryKey = 2;
$item->setAttributes(array('name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1), false);
$item->save(false); $item->save(false);
$item = new Item(); $item = new Item();
$item->setAttributes(array('id' => 3, 'name' => 'Ice Age', 'category_id' => 2), false); $item->primaryKey = 3;
$item->setAttributes(array('name' => 'Ice Age', 'category_id' => 2), false);
$item->save(false); $item->save(false);
$item = new Item(); $item = new Item();
$item->setAttributes(array('id' => 4, 'name' => 'Toy Story', 'category_id' => 2), false); $item->primaryKey = 4;
$item->setAttributes(array('name' => 'Toy Story', 'category_id' => 2), false);
$item->save(false); $item->save(false);
$item = new Item(); $item = new Item();
$item->setAttributes(array('id' => 5, 'name' => 'Cars', 'category_id' => 2), false); $item->primaryKey = 5;
$item->setAttributes(array('name' => 'Cars', 'category_id' => 2), false);
$item->save(false); $item->save(false);
$order = new Order(); $order = new Order();
$order->setAttributes(array('id' => 1, 'customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0), false); $order->primaryKey = 1;
$order->setAttributes(array('customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0), false);
$order->save(false); $order->save(false);
$order = new Order(); $order = new Order();
$order->setAttributes(array('id' => 2, 'customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0), false); $order->primaryKey = 2;
$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0), false);
$order->save(false); $order->save(false);
$order = new Order(); $order = new Order();
$order->setAttributes(array('id' => 3, 'customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0), false); $order->primaryKey = 3;
$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0), false);
$order->save(false); $order->save(false);
// $orderItem = new OrderItem(); // $orderItem = new OrderItem();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment