mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-20 00:20:44 +08:00
fixed offset and limit in redis AR
This commit is contained in:
@@ -107,7 +107,7 @@ class ActiveQuery extends \yii\base\Component
|
|||||||
$db = $modelClass::getDb();
|
$db = $modelClass::getDb();
|
||||||
if (($primaryKeys = $this->primaryKeys) === null) {
|
if (($primaryKeys = $this->primaryKeys) === null) {
|
||||||
$start = $this->offset === null ? 0 : $this->offset;
|
$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));
|
$primaryKeys = $db->executeCommand('LRANGE', array($modelClass::tableName(), $start, $end));
|
||||||
}
|
}
|
||||||
$rows = array();
|
$rows = array();
|
||||||
@@ -145,7 +145,7 @@ class ActiveQuery extends \yii\base\Component
|
|||||||
$db = $modelClass::getDb();
|
$db = $modelClass::getDb();
|
||||||
if (($primaryKeys = $this->primaryKeys) === null) {
|
if (($primaryKeys = $this->primaryKeys) === null) {
|
||||||
$start = $this->offset === null ? 0 : $this->offset;
|
$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);
|
$pk = reset($primaryKeys);
|
||||||
$key = $modelClass::tableName() . ':a:' . $modelClass::hashPk($pk);
|
$key = $modelClass::tableName() . ':a:' . $modelClass::hashPk($pk);
|
||||||
@@ -184,7 +184,13 @@ class ActiveQuery extends \yii\base\Component
|
|||||||
$modelClass = $this->modelClass;
|
$modelClass = $this->modelClass;
|
||||||
/** @var Connection $db */
|
/** @var Connection $db */
|
||||||
$db = $modelClass::getDb();
|
$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.
|
* Sets the LIMIT part of the query.
|
||||||
* TODO: refactor, it is duplicated from yii/db/Query
|
* TODO: refactor, it is duplicated from yii/db/Query
|
||||||
* @param integer $limit the limit
|
* @param integer $limit the limit
|
||||||
* @return Query the query object itself
|
* @return ActiveQuery the query object itself
|
||||||
*/
|
*/
|
||||||
public function limit($limit)
|
public function limit($limit)
|
||||||
{
|
{
|
||||||
@@ -237,7 +243,7 @@ class ActiveQuery extends \yii\base\Component
|
|||||||
* Sets the OFFSET part of the query.
|
* Sets the OFFSET part of the query.
|
||||||
* TODO: refactor, it is duplicated from yii/db/Query
|
* TODO: refactor, it is duplicated from yii/db/Query
|
||||||
* @param integer $offset the offset
|
* @param integer $offset the offset
|
||||||
* @return Query the query object itself
|
* @return ActiveQuery the query object itself
|
||||||
*/
|
*/
|
||||||
public function offset($offset)
|
public function offset($offset)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -114,6 +114,12 @@ class ActiveRecordTest extends RedisTestCase
|
|||||||
$customer = Customer::find(2);
|
$customer = Customer::find(2);
|
||||||
$this->assertTrue($customer instanceof Customer);
|
$this->assertTrue($customer instanceof Customer);
|
||||||
$this->assertEquals('user2', $customer->name);
|
$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
|
// find by column values
|
||||||
$customer = Customer::find(array('id' => 2));
|
$customer = Customer::find(array('id' => 2));
|
||||||
@@ -127,14 +133,7 @@ class ActiveRecordTest extends RedisTestCase
|
|||||||
$this->assertTrue($customer instanceof Customer);
|
$this->assertTrue($customer instanceof Customer);
|
||||||
$this->assertEquals(2, $customer->id);*/
|
$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
|
// 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(2, Customer::find()->where('id=1 OR id=2')->count());
|
||||||
$this->assertEquals(6, Customer::find()->sum('id'));
|
$this->assertEquals(6, Customer::find()->sum('id'));
|
||||||
$this->assertEquals(2, Customer::find()->average('id'));
|
$this->assertEquals(2, Customer::find()->average('id'));
|
||||||
@@ -163,6 +162,64 @@ class ActiveRecordTest extends RedisTestCase
|
|||||||
$this->assertTrue($customers['user3'] instanceof Customer);
|
$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()
|
// public function testFindLazy()
|
||||||
// {
|
// {
|
||||||
// /** @var $customer Customer */
|
// /** @var $customer Customer */
|
||||||
|
|||||||
Reference in New Issue
Block a user