Commit 426223af by Carsten Brandt

more query methods and fixes

parent 779d6b6e
...@@ -132,4 +132,24 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -132,4 +132,24 @@ class ActiveQuery extends Query implements ActiveQueryInterface
} }
return $model; 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;
}
}
} }
...@@ -213,6 +213,9 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord ...@@ -213,6 +213,9 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
*/ */
public static function updateAll($attributes, $condition = [], $params = []) public static function updateAll($attributes, $condition = [], $params = [])
{ {
if (empty($condition)) {
return 0;
}
$bulk = ''; $bulk = '';
foreach((array) $condition as $pk) { foreach((array) $condition as $pk) {
$action = Json::encode([ $action = Json::encode([
...@@ -258,8 +261,11 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord ...@@ -258,8 +261,11 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
* @param array $params this parameter is ignored in redis implementation. * @param array $params this parameter is ignored in redis implementation.
* @return integer the number of rows deleted * @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 = ''; $bulk = '';
foreach((array) $condition as $pk) { foreach((array) $condition as $pk) {
$bulk = Json::encode([ $bulk = Json::encode([
......
...@@ -274,7 +274,7 @@ class Command extends Component ...@@ -274,7 +274,7 @@ class Command extends Component
/** /**
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-flush.html * @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(); $response = $this->db->http()->post($this->createUrl([$index, '_flush']))->send();
return $response->getStatusCode() == 200; return $response->getStatusCode() == 200;
......
...@@ -87,20 +87,20 @@ class Query extends Component implements QueryInterface ...@@ -87,20 +87,20 @@ class Query extends Component implements QueryInterface
/** /**
* Returns the query result as a scalar value. * Returns the query result as a scalar value.
* The value returned will be the first column in the first row of the query results. * The value returned will be the specified attribute in the first record of the query results.
* @param string $column name of the column to select * @param string $attribute name of the attribute to select
* @param Connection $db the database connection used to execute the query. * @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used. * 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. * @return string the value of the specified attribute in the first record of the query result.
* False is returned if the query result is empty. * 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); $record = $this->one($db);
if ($record === null) { if ($record !== null) {
return false; return $record->$attribute;
} else { } else {
return $record->$column; return null;
} }
} }
......
...@@ -241,32 +241,32 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -241,32 +241,32 @@ class ActiveRecordTest extends ElasticSearchTestCase
public function testFindComplexCondition() public function testFindComplexCondition()
{ {
$this->assertEquals(2, Customer::find()->where(array('OR', array('id' => 1), array('id' => 2)))->count()); $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('id' => 1), array('id' => 2)))->all())); $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, Customer::find()->where(array('name' => array('user1','user2')))->count());
$this->assertEquals(2, count(Customer::find()->where(array('id' => array(1,2)))->all())); $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, 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('id' => array(2,3)), array('BETWEEN', 'status', 2, 4)))->all())); $this->assertEquals(1, count(Customer::find()->where(array('AND', array('name' => array('user2','user3')), array('BETWEEN', 'status', 2, 4)))->all()));
} }
public function testSum() // public function testSum()
{ // {
$this->assertEquals(6, OrderItem::find()->count()); // $this->assertEquals(6, OrderItem::find()->count());
$this->assertEquals(7, OrderItem::find()->sum('quantity')); // $this->assertEquals(7, OrderItem::find()->sum('quantity'));
} // }
public function testFindColumn() // public function testFindColumn()
{ // {
$this->assertEquals(array('user1', 'user2', 'user3'), Customer::find()->column('name')); // $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')); //// TODO $this->assertEquals(array('user3', 'user2', 'user1'), Customer::find()->orderBy(array('name' => Query::SORT_DESC))->column('name'));
} // }
public function testExists() public function testExists()
{ {
$this->assertTrue(Customer::find()->where(array('id' => 2))->exists()); $this->assertTrue(Customer::find()->where(array('name' => 'user1'))->exists());
$this->assertFalse(Customer::find()->where(array('id' => 5))->exists()); $this->assertFalse(Customer::find()->where(array('name' => 'user5'))->exists());
} }
// public function testFindLazy() // public function testFindLazy()
...@@ -393,19 +393,19 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -393,19 +393,19 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer->name = 'user4'; $customer->name = 'user4';
$customer->address = 'address4'; $customer->address = 'address4';
$this->assertNull($customer->id); $this->assertNull($customer->primaryKey);
$this->assertTrue($customer->isNewRecord); $this->assertTrue($customer->isNewRecord);
$customer->save(); $customer->save();
$this->assertNotNull($customer->id); $this->assertNotNull($customer->primaryKey);
$this->assertFalse($customer->isNewRecord); $this->assertFalse($customer->isNewRecord);
} }
public function testInsertPk() public function testInsertPk()
{ {
$customer = new Customer; $customer = new Customer;
$customer->id = 5; $customer->primaryKey = 5;
$customer->email = 'user5@example.com'; $customer->email = 'user5@example.com';
$customer->name = 'user5'; $customer->name = 'user5';
$customer->address = 'address5'; $customer->address = 'address5';
...@@ -414,7 +414,7 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -414,7 +414,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer->save(); $customer->save();
$this->assertEquals(5, $customer->id); $this->assertEquals(5, $customer->primaryKey);
$this->assertFalse($customer->isNewRecord); $this->assertFalse($customer->isNewRecord);
} }
...@@ -447,15 +447,12 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -447,15 +447,12 @@ class ActiveRecordTest extends ElasticSearchTestCase
{ {
$this->setExpectedException('yii\base\NotSupportedException'); $this->setExpectedException('yii\base\NotSupportedException');
$pk = array('id' => 2); $pk = array('primaryKey' => 2);
$orderItem = Order::find($pk); $orderItem = Order::find($pk);
$this->assertEquals(2, $orderItem->id); $this->assertEquals(2, $orderItem->primaryKey);
$orderItem->id = 13; $orderItem->primaryKey = 13;
$orderItem->save(); $orderItem->save();
$this->assertNull(OrderItem::find($pk));
$this->assertNotNull(OrderItem::find(array('id' => 13)));
} }
public function testDelete() public function testDelete()
...@@ -468,10 +465,12 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -468,10 +465,12 @@ class ActiveRecordTest extends ElasticSearchTestCase
$customer = Customer::find(2); $customer = Customer::find(2);
$this->assertNull($customer); $this->assertNull($customer);
Customer::getDb()->createCommand()->flushIndex('customers');
// deleteAll // deleteAll
$customers = Customer::find()->all(); $customers = Customer::find()->all();
$this->assertEquals(2, count($customers)); $this->assertEquals(2, count($customers));
$ret = Customer::deleteAll(); $ret = Customer::deleteAll([1,2,3]);
$this->assertEquals(2, $ret); $this->assertEquals(2, $ret);
$customers = Customer::find()->all(); $customers = Customer::find()->all();
$this->assertEquals(0, count($customers)); $this->assertEquals(0, count($customers));
......
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