Browse Source

more query methods and fixes

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
426223af1d
  1. 20
      framework/yii/elasticsearch/ActiveQuery.php
  2. 8
      framework/yii/elasticsearch/ActiveRecord.php
  3. 2
      framework/yii/elasticsearch/Command.php
  4. 16
      framework/yii/elasticsearch/Query.php
  5. 57
      tests/unit/framework/elasticsearch/ActiveRecordTest.php

20
framework/yii/elasticsearch/ActiveQuery.php

@ -132,4 +132,24 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
return $model;
}
/**
* Returns the query result as a scalar value.
* The value returned will be the specified attribute in the first record of the query results.
* @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return string the value of the specified attribute in the first record of the query result.
* Null is returned if the query result is empty.
*/
public function scalar($attribute, $db = null)
{
$record = $this->one($db);
if ($record !== null) {
return $record->$attribute;
} else {
return null;
}
}
}

8
framework/yii/elasticsearch/ActiveRecord.php

@ -213,6 +213,9 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function updateAll($attributes, $condition = [], $params = [])
{
if (empty($condition)) {
return 0;
}
$bulk = '';
foreach((array) $condition as $pk) {
$action = Json::encode([
@ -258,8 +261,11 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
* @param array $params this parameter is ignored in redis implementation.
* @return integer the number of rows deleted
*/
public static function deleteAll($condition = null, $params = [])
public static function deleteAll($condition = [], $params = [])
{
if (empty($condition)) {
return 0;
}
$bulk = '';
foreach((array) $condition as $pk) {
$bulk = Json::encode([

2
framework/yii/elasticsearch/Command.php

@ -274,7 +274,7 @@ class Command extends Component
/**
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html
*/
public function flushIndex($index)
public function flushIndex($index = '_all')
{
$response = $this->db->http()->post($this->createUrl([$index, '_flush']))->send();
return $response->getStatusCode() == 200;

16
framework/yii/elasticsearch/Query.php

@ -87,20 +87,20 @@ class Query extends Component implements QueryInterface
/**
* Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results.
* @param string $column name of the column to select
* The value returned will be the specified attribute in the first record of the query results.
* @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @return string|boolean the value of the first column in the first row of the query result.
* False is returned if the query result is empty.
* @return string the value of the specified attribute in the first record of the query result.
* Null is returned if the query result is empty.
*/
public function scalar($column, $db = null)
public function scalar($attribute, $db = null)
{
$record = $this->one($db);
if ($record === null) {
return false;
if ($record !== null) {
return $record->$attribute;
} else {
return $record->$column;
return null;
}
}

57
tests/unit/framework/elasticsearch/ActiveRecordTest.php

@ -241,32 +241,32 @@ class ActiveRecordTest extends ElasticSearchTestCase
public function testFindComplexCondition()
{
$this->assertEquals(2, Customer::find()->where(array('OR', array('id' => 1), array('id' => 2)))->count());
$this->assertEquals(2, count(Customer::find()->where(array('OR', array('id' => 1), array('id' => 2)))->all()));
$this->assertEquals(2, Customer::find()->where(array('OR', array('name' => 'user1'), array('name' => 'user2')))->count());
$this->assertEquals(2, count(Customer::find()->where(array('OR', array('name' => 'user1'), array('name' => 'user2')))->all()));
$this->assertEquals(2, Customer::find()->where(array('id' => array(1,2)))->count());
$this->assertEquals(2, count(Customer::find()->where(array('id' => array(1,2)))->all()));
$this->assertEquals(2, Customer::find()->where(array('name' => array('user1','user2')))->count());
$this->assertEquals(2, count(Customer::find()->where(array('name' => array('user1','user2')))->all()));
$this->assertEquals(1, Customer::find()->where(array('AND', array('id' => array(2,3)), array('BETWEEN', 'status', 2, 4)))->count());
$this->assertEquals(1, count(Customer::find()->where(array('AND', array('id' => array(2,3)), array('BETWEEN', 'status', 2, 4)))->all()));
$this->assertEquals(1, Customer::find()->where(array('AND', array('name' => array('user2','user3')), array('BETWEEN', 'status', 2, 4)))->count());
$this->assertEquals(1, count(Customer::find()->where(array('AND', array('name' => array('user2','user3')), array('BETWEEN', 'status', 2, 4)))->all()));
}
public function testSum()
{
$this->assertEquals(6, OrderItem::find()->count());
$this->assertEquals(7, OrderItem::find()->sum('quantity'));
}
// public function testSum()
// {
// $this->assertEquals(6, OrderItem::find()->count());
// $this->assertEquals(7, OrderItem::find()->sum('quantity'));
// }
public function testFindColumn()
{
$this->assertEquals(array('user1', 'user2', 'user3'), Customer::find()->column('name'));
// TODO $this->assertEquals(array('user3', 'user2', 'user1'), Customer::find()->orderBy(array('name' => Query::SORT_DESC))->column('name'));
}
// public function testFindColumn()
// {
// $this->assertEquals(array('user1', 'user2', 'user3'), Customer::find()->column('name'));
//// TODO $this->assertEquals(array('user3', 'user2', 'user1'), Customer::find()->orderBy(array('name' => Query::SORT_DESC))->column('name'));
// }
public function testExists()
{
$this->assertTrue(Customer::find()->where(array('id' => 2))->exists());
$this->assertFalse(Customer::find()->where(array('id' => 5))->exists());
$this->assertTrue(Customer::find()->where(array('name' => 'user1'))->exists());
$this->assertFalse(Customer::find()->where(array('name' => 'user5'))->exists());
}
// public function testFindLazy()
@ -393,19 +393,19 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer->name = 'user4';
$customer->address = 'address4';
$this->assertNull($customer->id);
$this->assertNull($customer->primaryKey);
$this->assertTrue($customer->isNewRecord);
$customer->save();
$this->assertNotNull($customer->id);
$this->assertNotNull($customer->primaryKey);
$this->assertFalse($customer->isNewRecord);
}
public function testInsertPk()
{
$customer = new Customer;
$customer->id = 5;
$customer->primaryKey = 5;
$customer->email = 'user5@example.com';
$customer->name = 'user5';
$customer->address = 'address5';
@ -414,7 +414,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer->save();
$this->assertEquals(5, $customer->id);
$this->assertEquals(5, $customer->primaryKey);
$this->assertFalse($customer->isNewRecord);
}
@ -447,15 +447,12 @@ class ActiveRecordTest extends ElasticSearchTestCase
{
$this->setExpectedException('yii\base\NotSupportedException');
$pk = array('id' => 2);
$pk = array('primaryKey' => 2);
$orderItem = Order::find($pk);
$this->assertEquals(2, $orderItem->id);
$this->assertEquals(2, $orderItem->primaryKey);
$orderItem->id = 13;
$orderItem->primaryKey = 13;
$orderItem->save();
$this->assertNull(OrderItem::find($pk));
$this->assertNotNull(OrderItem::find(array('id' => 13)));
}
public function testDelete()
@ -468,10 +465,12 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer = Customer::find(2);
$this->assertNull($customer);
Customer::getDb()->createCommand()->flushIndex('customers');
// deleteAll
$customers = Customer::find()->all();
$this->assertEquals(2, count($customers));
$ret = Customer::deleteAll();
$ret = Customer::deleteAll([1,2,3]);
$this->assertEquals(2, $ret);
$customers = Customer::find()->all();
$this->assertEquals(0, count($customers));

Loading…
Cancel
Save