Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
72889128
Commit
72889128
authored
Sep 18, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed offset and limit in redis AR
parent
563171eb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
12 deletions
+75
-12
ActiveQuery.php
framework/yii/redis/ActiveQuery.php
+11
-5
ActiveRecordTest.php
tests/unit/framework/redis/ActiveRecordTest.php
+64
-7
No files found.
framework/yii/redis/ActiveQuery.php
View file @
72889128
...
...
@@ -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
Active
Query 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
Active
Query the query object itself
*/
public
function
offset
(
$offset
)
{
...
...
tests/unit/framework/redis/ActiveRecordTest.php
View file @
72889128
...
...
@@ -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 */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment