diff --git a/extensions/yii/elasticsearch/Query.php b/extensions/yii/elasticsearch/Query.php index 79e58c3..f404fc1 100644 --- a/extensions/yii/elasticsearch/Query.php +++ b/extensions/yii/elasticsearch/Query.php @@ -283,13 +283,7 @@ class Query extends Component implements QueryInterface $options = []; $options['search_type'] = 'count'; - $count = $this->createCommand($db)->search($options)['hits']['total']; - if ($this->limit === null && $this->offset === null) { - return $count; - } elseif ($this->offset !== null) { - $count = $this->offset < $count ? $count - $this->offset : 0; - } - return $this->limit === null ? $count : ($this->limit > $count ? $count : $this->limit); + return $this->createCommand($db)->search($options)['hits']['total']; } /** diff --git a/extensions/yii/redis/ActiveQuery.php b/extensions/yii/redis/ActiveQuery.php index 607b18e..b64beba 100644 --- a/extensions/yii/redis/ActiveQuery.php +++ b/extensions/yii/redis/ActiveQuery.php @@ -126,7 +126,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface */ public function count($q = '*', $db = null) { - if ($this->offset === null && $this->limit === null && $this->where === null) { + if ($this->where === null) { /** @var ActiveRecord $modelClass */ $modelClass = $this->modelClass; if ($db === null) { @@ -291,11 +291,17 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface /** @var ActiveRecord $modelClass */ $modelClass = $this->modelClass; - $start = $this->offset === null ? 0 : $this->offset; + if ($type == 'Count') { + $start = 0; + $limit = null; + } else { + $start = $this->offset === null ? 0 : $this->offset; + $limit = $this->limit; + } $i = 0; $data = []; foreach($pks as $pk) { - if (++$i > $start && ($this->limit === null || $i <= $start + $this->limit)) { + if (++$i > $start && ($limit === null || $i <= $start + $limit)) { $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk); $result = $db->executeCommand('HGETALL', [$key]); if (!empty($result)) { diff --git a/framework/yii/db/Query.php b/framework/yii/db/Query.php index 0d81887..ee24c2f 100644 --- a/framework/yii/db/Query.php +++ b/framework/yii/db/Query.php @@ -253,7 +253,11 @@ class Query extends Component implements QueryInterface */ public function exists($db = null) { - return $this->queryScalar(new Expression('1'), $db) !== false; + $select = $this->select; + $this->select = [new Expression('1')]; + $command = $this->createCommand($db); + $this->select = $select; + return $command->queryScalar() !== false; } /** @@ -266,9 +270,18 @@ class Query extends Component implements QueryInterface private function queryScalar($selectExpression, $db) { $select = $this->select; + $limit = $this->limit; + $offset = $this->offset; + $this->select = [$selectExpression]; + $this->limit = null; + $this->offset = null; $command = $this->createCommand($db); + $this->select = $select; + $this->limit = $limit; + $this->offset = $offset; + return $command->queryScalar(); } diff --git a/tests/unit/extensions/redis/ActiveRecordTest.php b/tests/unit/extensions/redis/ActiveRecordTest.php index f3cbbdc..3916f9d 100644 --- a/tests/unit/extensions/redis/ActiveRecordTest.php +++ b/tests/unit/extensions/redis/ActiveRecordTest.php @@ -205,14 +205,6 @@ class ActiveRecordTest extends RedisTestCase $this->assertEquals(2, $order->items[1]->id); } - 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 testFindColumn() { $this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->column('name')); diff --git a/tests/unit/framework/ar/ActiveRecordTestTrait.php b/tests/unit/framework/ar/ActiveRecordTestTrait.php index 3f1739e..50a5f81 100644 --- a/tests/unit/framework/ar/ActiveRecordTestTrait.php +++ b/tests/unit/framework/ar/ActiveRecordTestTrait.php @@ -287,10 +287,17 @@ trait ActiveRecordTestTrait { /** @var TestCase|ActiveRecordTestTrait $this */ $this->assertEquals(3, $this->callCustomerFind()->count()); - // TODO should limit have effect on count() -// $this->assertEquals(1, $this->callCustomerFind()->limit(1)->count()); -// $this->assertEquals(2, $this->callCustomerFind()->limit(2)->count()); -// $this->assertEquals(1, $this->callCustomerFind()->offset(2)->limit(2)->count()); + + $this->assertEquals(1, $this->callCustomerFind()->where(['id' => 1])->count()); + $this->assertEquals(2, $this->callCustomerFind()->where(['id' => [1, 2]])->count()); + $this->assertEquals(2, $this->callCustomerFind()->where(['id' => [1, 2]])->offset(1)->count()); + $this->assertEquals(2, $this->callCustomerFind()->where(['id' => [1, 2]])->offset(2)->count()); + + // limit should have no effect on count() + $this->assertEquals(3, $this->callCustomerFind()->limit(1)->count()); + $this->assertEquals(3, $this->callCustomerFind()->limit(2)->count()); + $this->assertEquals(3, $this->callCustomerFind()->limit(10)->count()); + $this->assertEquals(3, $this->callCustomerFind()->offset(2)->limit(2)->count()); } public function testFindLimit() @@ -371,6 +378,10 @@ trait ActiveRecordTestTrait $this->assertFalse($this->callCustomerFind()->where(['id' => 5])->exists()); $this->assertTrue($this->callCustomerFind()->where(['name' => 'user1'])->exists()); $this->assertFalse($this->callCustomerFind()->where(['name' => 'user5'])->exists()); + + $this->assertTrue($this->callCustomerFind()->where(['id' => [2,3]])->exists()); + $this->assertTrue($this->callCustomerFind()->where(['id' => [2,3]])->offset(1)->exists()); + $this->assertFalse($this->callCustomerFind()->where(['id' => [2,3]])->offset(2)->exists()); } public function testFindLazy()