Commit af5d87ac by Carsten Brandt

renamed elasticsearch PK to id

parent 64359373
......@@ -92,7 +92,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
if ($this->asArray) {
foreach($models as $key => $model) {
$models[$key] = $model['_source'];
$models[$key]['primaryKey'] = $model['_id'];
$models[$key][ActiveRecord::PRIMARY_KEY_NAME] = $model['_id'];
}
}
if (!empty($this->with)) {
......@@ -116,7 +116,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
if ($this->asArray) {
$model = $result['_source'];
$model['primaryKey'] = $result['_id'];
$model[ActiveRecord::PRIMARY_KEY_NAME] = $result['_id'];
} else {
/** @var ActiveRecord $class */
$class = $this->modelClass;
......@@ -137,7 +137,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
{
$record = parent::one($db);
if ($record !== false) {
if ($field == 'primaryKey') {
if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
return $record['_id'];
} elseif (isset($record['_source'][$field])) {
return $record['_source'][$field];
......@@ -151,7 +151,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public function column($field, $db = null)
{
if ($field == 'primaryKey') {
if ($field == ActiveRecord::PRIMARY_KEY_NAME) {
$command = $this->createCommand($db);
$command->queryParts['fields'] = [];
$rows = $command->queryAll()['hits'];
......
......@@ -27,6 +27,8 @@ use yii\helpers\StringHelper;
*/
class ActiveRecord extends \yii\db\ActiveRecord
{
const PRIMARY_KEY_NAME = 'id';
private $_id;
private $_version;
......@@ -48,8 +50,8 @@ class ActiveRecord extends \yii\db\ActiveRecord
{
$query = static::createQuery();
if (is_array($q)) {
if (count($q) == 1 && (array_key_exists('primaryKey', $q))) {
return static::get($q['primaryKey']);
if (count($q) == 1 && (array_key_exists(ActiveRecord::PRIMARY_KEY_NAME, $q))) {
return static::get($q[ActiveRecord::PRIMARY_KEY_NAME]);
}
return $query->where($q)->one();
} elseif ($q !== null) {
......@@ -68,7 +70,6 @@ class ActiveRecord extends \yii\db\ActiveRecord
* for more details on these options.
* @return static|null The record instance or null if it was not found.
*/
public static function get($primaryKey, $options = [])
{
if ($primaryKey === null) {
......@@ -132,12 +133,17 @@ class ActiveRecord extends \yii\db\ActiveRecord
// TODO implement copy and move as pk change is not possible
public function getId()
{
return $this->_id;
}
/**
* Sets the primary key
* @param mixed $value
* @throws \yii\base\InvalidCallException when record is not new
*/
public function setPrimaryKey($value)
public function setId($value)
{
if ($this->isNewRecord) {
$this->_id = $value;
......@@ -152,7 +158,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
public function getPrimaryKey($asArray = false)
{
if ($asArray) {
return ['primaryKey' => $this->_id];
return [ActiveRecord::PRIMARY_KEY_NAME => $this->_id];
} else {
return $this->_id;
}
......@@ -165,7 +171,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
{
$id = $this->isNewRecord ? null : $this->_id;
if ($asArray) {
return ['primaryKey' => $id];
return [ActiveRecord::PRIMARY_KEY_NAME => $id];
} else {
return $this->_id;
}
......@@ -180,7 +186,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function primaryKey()
{
return ['primaryKey'];
return [ActiveRecord::PRIMARY_KEY_NAME];
}
/**
......@@ -218,7 +224,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function create($row)
{
$row['_source']['primaryKey'] = $row['_id'];
$row['_source'][ActiveRecord::PRIMARY_KEY_NAME] = $row['_id'];
$record = parent::create($row['_source']);
return $record;
}
......@@ -317,10 +323,10 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function updateAll($attributes, $condition = [], $params = [])
{
if (count($condition) == 1 && isset($condition['primaryKey'])) {
$primaryKeys = (array) $condition['primaryKey'];
if (count($condition) == 1 && isset($condition[ActiveRecord::PRIMARY_KEY_NAME])) {
$primaryKeys = (array) $condition[ActiveRecord::PRIMARY_KEY_NAME];
} else {
$primaryKeys = static::find()->where($condition)->column('primaryKey');
$primaryKeys = static::find()->where($condition)->column(ActiveRecord::PRIMARY_KEY_NAME);
}
if (empty($primaryKeys)) {
return 0;
......@@ -372,10 +378,10 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function deleteAll($condition = [], $params = [])
{
if (count($condition) == 1 && isset($condition['primaryKey'])) {
$primaryKeys = (array) $condition['primaryKey'];
if (count($condition) == 1 && isset($condition[ActiveRecord::PRIMARY_KEY_NAME])) {
$primaryKeys = (array) $condition[ActiveRecord::PRIMARY_KEY_NAME];
} else {
$primaryKeys = static::find()->where($condition)->column('primaryKey');
$primaryKeys = static::find()->where($condition)->column(ActiveRecord::PRIMARY_KEY_NAME);
}
if (empty($primaryKeys)) {
return 0;
......
......@@ -96,13 +96,21 @@ class QueryBuilder extends \yii\base\Object
}
$orders = [];
foreach ($columns as $name => $direction) {
if (is_string($direction)) {
$column = $direction;
$direction = SORT_ASC;
} else {
$column = $name;
}
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
$column = '_id';
}
// allow elasticsearch extended syntax as described in http://www.elasticsearch.org/guide/reference/api/search/sort/
if (is_array($direction)) {
$orders[] = array($name => $direction);
} elseif (is_string($direction)) {
$orders[] = $direction;
$orders[] = [$column => $direction];
} else {
$orders[] = array($name => ($direction === SORT_DESC ? 'desc' : 'asc'));
$orders[] = [$column => ($direction === SORT_DESC ? 'desc' : 'asc')];
}
}
return $orders;
......@@ -155,7 +163,7 @@ class QueryBuilder extends \yii\base\Object
{
$parts = [];
foreach($condition as $attribute => $value) {
if ($attribute == 'primaryKey') {
if ($attribute == ActiveRecord::PRIMARY_KEY_NAME) {
if ($value == null) { // there is no null pk
$parts[] = ['script' => ['script' => '0==1']];
} else {
......@@ -201,7 +209,7 @@ class QueryBuilder extends \yii\base\Object
}
list($column, $value1, $value2) = $operands;
if ($column == 'primaryKey') {
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
throw new NotSupportedException('Between condition is not supported for primaryKey.');
}
$filter = ['range' => [$column => ['gte' => $value1, 'lte' => $value2]]];
......@@ -240,7 +248,7 @@ class QueryBuilder extends \yii\base\Object
unset($values[$i]);
}
}
if ($column == 'primaryKey') {
if ($column == ActiveRecord::PRIMARY_KEY_NAME) {
if (empty($values) && $canBeNull) { // there is no null pk
$filter = ['script' => ['script' => '0==1']];
} else {
......
......@@ -26,7 +26,7 @@ class Customer extends ActiveRecord
public function getOrders()
{
return $this->hasMany(Order::className(), array('customer_id' => 'primaryKey'))->orderBy('create_time');
return $this->hasMany(Order::className(), array('customer_id' => ActiveRecord::PRIMARY_KEY_NAME))->orderBy('create_time');
}
public static function active($query)
......
......@@ -19,17 +19,17 @@ class Order extends ActiveRecord
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['primaryKey' => 'customer_id']);
return $this->hasOne(Customer::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'customer_id']);
}
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'primaryKey']);
return $this->hasMany(OrderItem::className(), ['order_id' => ActiveRecord::PRIMARY_KEY_NAME]);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['primaryKey' => 'item_id'])
return $this->hasMany(Item::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'item_id'])
->via('orderItems')->orderBy('name');
}
......@@ -51,8 +51,8 @@ class Order extends ActiveRecord
// public function getBooks()
// {
// return $this->hasMany('Item', ['primaryKey' => 'item_id'])
// ->viaTable('tbl_order_item', ['order_id' => 'primaryKey'])
// return $this->hasMany('Item', [ActiveRecord::PRIMARY_KEY_NAME => 'item_id'])
// ->viaTable('tbl_order_item', ['order_id' => ActiveRecord::PRIMARY_KEY_NAME])
// ->where(['category_id' => 1]);
// }
......
......@@ -19,11 +19,11 @@ class OrderItem extends ActiveRecord
public function getOrder()
{
return $this->hasOne(Order::className(), ['primaryKey' => 'order_id']);
return $this->hasOne(Order::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'order_id']);
}
public function getItem()
{
return $this->hasOne(Item::className(), ['primaryKey' => 'item_id']);
return $this->hasOne(Item::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'item_id']);
}
}
......@@ -44,15 +44,15 @@ class ActiveRecordTest extends ElasticSearchTestCase
$db->http()->delete('_all')->send();
$customer = new Customer();
$customer->primaryKey = 1;
$customer->id = 1;
$customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false);
$customer->save(false);
$customer = new Customer();
$customer->primaryKey = 2;
$customer->id = 2;
$customer->setAttributes(['email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1], false);
$customer->save(false);
$customer = new Customer();
$customer->primaryKey = 3;
$customer->id = 3;
$customer->setAttributes(['email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2], false);
$customer->save(false);
......@@ -60,36 +60,36 @@ class ActiveRecordTest extends ElasticSearchTestCase
// INSERT INTO tbl_category (name) VALUES ('Movies');
$item = new Item();
$item->primaryKey = 1;
$item->id = 1;
$item->setAttributes(['name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1], false);
$item->save(false);
$item = new Item();
$item->primaryKey = 2;
$item->id = 2;
$item->setAttributes(['name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1], false);
$item->save(false);
$item = new Item();
$item->primaryKey = 3;
$item->id = 3;
$item->setAttributes(['name' => 'Ice Age', 'category_id' => 2], false);
$item->save(false);
$item = new Item();
$item->primaryKey = 4;
$item->id = 4;
$item->setAttributes(['name' => 'Toy Story', 'category_id' => 2], false);
$item->save(false);
$item = new Item();
$item->primaryKey = 5;
$item->id = 5;
$item->setAttributes(['name' => 'Cars', 'category_id' => 2], false);
$item->save(false);
$order = new Order();
$order->primaryKey = 1;
$order->id = 1;
$order->setAttributes(['customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0], false);
$order->save(false);
$order = new Order();
$order->primaryKey = 2;
$order->id = 2;
$order->setAttributes(['customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0], false);
$order->save(false);
$order = new Order();
$order->primaryKey = 3;
$order->id = 3;
$order->setAttributes(['customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0], false);
$order->save(false);
......@@ -133,17 +133,17 @@ class ActiveRecordTest extends ElasticSearchTestCase
// find all asArray
$customers = Customer::find()->asArray()->all();
$this->assertEquals(3, count($customers));
$this->assertArrayHasKey('primaryKey', $customers[0]);
$this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[0]);
$this->assertArrayHasKey('name', $customers[0]);
$this->assertArrayHasKey('email', $customers[0]);
$this->assertArrayHasKey('address', $customers[0]);
$this->assertArrayHasKey('status', $customers[0]);
$this->assertArrayHasKey('primaryKey', $customers[1]);
$this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[1]);
$this->assertArrayHasKey('name', $customers[1]);
$this->assertArrayHasKey('email', $customers[1]);
$this->assertArrayHasKey('address', $customers[1]);
$this->assertArrayHasKey('status', $customers[1]);
$this->assertArrayHasKey('primaryKey', $customers[2]);
$this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[2]);
$this->assertArrayHasKey('name', $customers[2]);
$this->assertArrayHasKey('email', $customers[2]);
$this->assertArrayHasKey('address', $customers[2]);
......@@ -161,16 +161,16 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this->assertEquals('user3', $customerName);
$customerName = Customer::find()->where(['status' => 2])->scalar('noname');
$this->assertNull($customerName);
$customerId = Customer::find()->where(['status' => 2])->scalar('primaryKey');
$customerId = Customer::find()->where(['status' => 2])->scalar(ActiveRecord::PRIMARY_KEY_NAME);
$this->assertEquals(3, $customerId);
// find by column values
$customer = Customer::find(['name' => 'user2']);
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
$customer = Customer::find(['name' => 'user1', 'primaryKey' => 2]);
$customer = Customer::find(['name' => 'user1', ActiveRecord::PRIMARY_KEY_NAME => 2]);
$this->assertNull($customer);
$customer = Customer::find(['primaryKey' => 5]);
$customer = Customer::find([ActiveRecord::PRIMARY_KEY_NAME => 5]);
$this->assertNull($customer);
$customer = Customer::find(['name' => 'user5']);
$this->assertNull($customer);
......@@ -182,7 +182,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
// find count
$this->assertEquals(3, Customer::find()->count());
$this->assertEquals(2, Customer::find()->where(['or', ['primaryKey' => 1], ['primaryKey' => 2]])->count());
$this->assertEquals(2, Customer::find()->where(['or', [ActiveRecord::PRIMARY_KEY_NAME => 1], [ActiveRecord::PRIMARY_KEY_NAME => 2]])->count());
// $this->assertEquals(6, Customer::find()->sum('id'));
// $this->assertEquals(2, Customer::find()->average('id'));
// $this->assertEquals(1, Customer::find()->min('id'));
......@@ -199,7 +199,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
'name' => 'user2',
'address' => 'address2',
'status' => '1',
'primaryKey' => 2,
ActiveRecord::PRIMARY_KEY_NAME => 2,
), $customer);
// indexBy
......@@ -257,7 +257,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$orders = $customer->getOrders()->where(['between', 'create_time', 1325334000, 1325400000])->all();
$this->assertEquals(1, count($orders));
$this->assertEquals(2, $orders[0]->primaryKey);
$this->assertEquals(2, $orders[0]->id);
}
public function testFindEagerViaRelation()
......@@ -266,16 +266,16 @@ class ActiveRecordTest extends ElasticSearchTestCase
$orders = Order::find()->with('items')->orderBy('create_time')->all();
$this->assertEquals(3, count($orders));
$order = $orders[0];
$this->assertEquals(1, $order->primaryKey);
$this->assertEquals(1, $order->id);
$this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->primaryKey);
$this->assertEquals(2, $order->items[1]->primaryKey);
$this->assertEquals(1, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id);
}
public function testInsertNoPk()
{
$this->assertEquals(['primaryKey'], Customer::primaryKey());
$this->assertEquals([ActiveRecord::PRIMARY_KEY_NAME], Customer::primaryKey());
$pkName = ActiveRecord::PRIMARY_KEY_NAME;
$customer = new Customer;
$customer->email = 'user4@example.com';
......@@ -284,20 +284,25 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this->assertNull($customer->primaryKey);
$this->assertNull($customer->oldPrimaryKey);
$this->assertNull($customer->$pkName);
$this->assertTrue($customer->isNewRecord);
$customer->save();
$this->assertNotNull($customer->primaryKey);
$this->assertNotNull($customer->oldPrimaryKey);
$this->assertNotNull($customer->$pkName);
$this->assertEquals($customer->primaryKey, $customer->oldPrimaryKey);
$this->assertEquals($customer->primaryKey, $customer->$pkName);
$this->assertFalse($customer->isNewRecord);
}
public function testInsertPk()
{
$pkName = ActiveRecord::PRIMARY_KEY_NAME;
$customer = new Customer;
$customer->primaryKey = 5;
$customer->$pkName = 5;
$customer->email = 'user5@example.com';
$customer->name = 'user5';
$customer->address = 'address5';
......@@ -307,17 +312,23 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer->save();
$this->assertEquals(5, $customer->primaryKey);
$this->assertEquals(5, $customer->oldPrimaryKey);
$this->assertEquals(5, $customer->$pkName);
$this->assertFalse($customer->isNewRecord);
}
public function testUpdatePk()
{
$pk = ['primaryKey' => 2];
$pkName = ActiveRecord::PRIMARY_KEY_NAME;
$pk = [$pkName => 2];
$orderItem = Order::find($pk);
$this->assertEquals(2, $orderItem->primaryKey);
$this->assertEquals(2, $orderItem->oldPrimaryKey);
$this->assertEquals(2, $orderItem->$pkName);
$this->setExpectedException('yii\base\InvalidCallException');
$orderItem->primaryKey = 13;
$orderItem->$pkName = 13;
$orderItem->save();
}
}
\ No newline at end of file
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