From 72889128fc18a5c38c57cb52c5d305aa2a45f0bf Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 18 Sep 2013 17:49:56 +0200 Subject: [PATCH] fixed offset and limit in redis AR --- framework/yii/redis/ActiveQuery.php | 16 ++++-- tests/unit/framework/redis/ActiveRecordTest.php | 71 ++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/framework/yii/redis/ActiveQuery.php b/framework/yii/redis/ActiveQuery.php index 54b7d31..f252898 100644 --- a/framework/yii/redis/ActiveQuery.php +++ b/framework/yii/redis/ActiveQuery.php @@ -107,7 +107,7 @@ class ActiveQuery extends \yii\base\Component $db = $modelClass::getDb(); if (($primaryKeys = $this->primaryKeys) === null) { $start = $this->offset === null ? 0 : $this->offset; - $end = $this->limit === null ? -1 : $start + $this->limit; + $end = $this->limit === null ? -1 : $start + $this->limit - 1; $primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $end)); } $rows = array(); @@ -145,7 +145,7 @@ class ActiveQuery extends \yii\base\Component $db = $modelClass::getDb(); if (($primaryKeys = $this->primaryKeys) === null) { $start = $this->offset === null ? 0 : $this->offset; - $primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $start + 1)); + $primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $start)); } $pk = reset($primaryKeys); $key = $modelClass::tableName() . ':a:' . $modelClass::hashPk($pk); @@ -184,7 +184,13 @@ class ActiveQuery extends \yii\base\Component $modelClass = $this->modelClass; /** @var Connection $db */ $db = $modelClass::getDb(); - return $db->executeCommand('LLEN', array($modelClass::tableName())); + if ($this->offset === null && $this->limit === null) { + return $db->executeCommand('LLEN', array($modelClass::tableName())); + } else { + $start = $this->offset === null ? 0 : $this->offset; + $end = $this->limit === null ? -1 : $start + $this->limit - 1; + return count($db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $end))); + } } /** @@ -225,7 +231,7 @@ class ActiveQuery extends \yii\base\Component * Sets the LIMIT part of the query. * TODO: refactor, it is duplicated from yii/db/Query * @param integer $limit the limit - * @return Query the query object itself + * @return ActiveQuery the query object itself */ public function limit($limit) { @@ -237,7 +243,7 @@ class ActiveQuery extends \yii\base\Component * Sets the OFFSET part of the query. * TODO: refactor, it is duplicated from yii/db/Query * @param integer $offset the offset - * @return Query the query object itself + * @return ActiveQuery the query object itself */ public function offset($offset) { diff --git a/tests/unit/framework/redis/ActiveRecordTest.php b/tests/unit/framework/redis/ActiveRecordTest.php index 2587878..53ea548 100644 --- a/tests/unit/framework/redis/ActiveRecordTest.php +++ b/tests/unit/framework/redis/ActiveRecordTest.php @@ -114,6 +114,12 @@ class ActiveRecordTest extends RedisTestCase $customer = Customer::find(2); $this->assertTrue($customer instanceof Customer); $this->assertEquals('user2', $customer->name); + $customer = Customer::find(5); + $this->assertNull($customer); + + // query scalar + $customerName = Customer::find()->primaryKeys(2)->scalar('name'); + $this->assertEquals('user2', $customerName); // find by column values $customer = Customer::find(array('id' => 2)); @@ -127,14 +133,7 @@ class ActiveRecordTest extends RedisTestCase $this->assertTrue($customer instanceof Customer); $this->assertEquals(2, $customer->id);*/ - // find custom column -/* $customer = Customer::find()->select(array('*', '(status*2) AS status2')) - ->where(array('name' => 'user3'))->one(); - $this->assertEquals(3, $customer->id); - $this->assertEquals(4, $customer->status2);*/ - // find count, sum, average, min, max, scalar - $this->assertEquals(3, Customer::find()->count()); /* $this->assertEquals(2, Customer::find()->where('id=1 OR id=2')->count()); $this->assertEquals(6, Customer::find()->sum('id')); $this->assertEquals(2, Customer::find()->average('id')); @@ -163,6 +162,64 @@ class ActiveRecordTest extends RedisTestCase $this->assertTrue($customers['user3'] instanceof Customer); } + public function testFindCount() + { + $this->assertEquals(3, Customer::find()->count()); + $this->assertEquals(1, Customer::find()->limit(1)->count()); + $this->assertEquals(2, Customer::find()->limit(2)->count()); + $this->assertEquals(1, Customer::find()->offset(2)->limit(2)->count()); + } + + public function testFindLimit() + { + // all() + $customers = Customer::find()->all(); + $this->assertEquals(3, count($customers)); + + $customers = Customer::find()->limit(1)->all(); + $this->assertEquals(1, count($customers)); + $this->assertEquals('user1', $customers[0]->name); + + $customers = Customer::find()->limit(1)->offset(1)->all(); + $this->assertEquals(1, count($customers)); + $this->assertEquals('user2', $customers[0]->name); + + $customers = Customer::find()->limit(1)->offset(2)->all(); + $this->assertEquals(1, count($customers)); + $this->assertEquals('user3', $customers[0]->name); + + $customers = Customer::find()->limit(2)->offset(1)->all(); + $this->assertEquals(2, count($customers)); + $this->assertEquals('user2', $customers[0]->name); + $this->assertEquals('user3', $customers[1]->name); + + $customers = Customer::find()->limit(2)->offset(3)->all(); + $this->assertEquals(0, count($customers)); + + // one() + $customer = Customer::find()->one(); + $this->assertEquals('user1', $customer->name); + + $customer = Customer::find()->offset(0)->one(); + $this->assertEquals('user1', $customer->name); + + $customer = Customer::find()->offset(1)->one(); + $this->assertEquals('user2', $customer->name); + + $customer = Customer::find()->offset(2)->one(); + $this->assertEquals('user3', $customer->name); + + $customer = Customer::find()->offset(3)->one(); + $this->assertNull($customer); + + } + + public function testExists() + { + $this->assertTrue(Customer::find()->primaryKeys(2)->exists()); + $this->assertFalse(Customer::find()->primaryKeys(5)->exists()); + } + // public function testFindLazy() // { // /** @var $customer Customer */