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
b4612637
Commit
b4612637
authored
Dec 18, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make count behave like in SQL
also fixed count behavior according to limit and offset. fixes #1458
parent
617de8ac
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
23 deletions
+39
-23
Query.php
extensions/yii/elasticsearch/Query.php
+1
-7
ActiveQuery.php
extensions/yii/redis/ActiveQuery.php
+9
-3
Query.php
framework/yii/db/Query.php
+14
-1
ActiveRecordTest.php
tests/unit/extensions/redis/ActiveRecordTest.php
+0
-8
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+15
-4
No files found.
extensions/yii/elasticsearch/Query.php
View file @
b4612637
...
...
@@ -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'
];
}
/**
...
...
extensions/yii/redis/ActiveQuery.php
View file @
b4612637
...
...
@@ -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
))
{
...
...
framework/yii/db/Query.php
View file @
b4612637
...
...
@@ -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
();
}
...
...
tests/unit/extensions/redis/ActiveRecordTest.php
View file @
b4612637
...
...
@@ -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'
));
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
b4612637
...
...
@@ -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
()
...
...
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