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
a53acdf6
Commit
a53acdf6
authored
Mar 01, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
....
parent
409eab13
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
8 deletions
+52
-8
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+13
-4
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+12
-0
Customer.php
tests/unit/data/ar/Customer.php
+13
-0
mysql.sql
tests/unit/data/mysql.sql
+4
-3
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+10
-1
No files found.
framework/db/ar/ActiveFinder.php
View file @
a53acdf6
...
...
@@ -188,13 +188,22 @@ class ActiveFinder extends \yii\base\Object
protected
function
applyScopes
(
$query
)
{
if
(
is_array
(
$query
->
scopes
))
{
foreach
(
$query
->
scopes
as
$scope
=>
$params
)
{
if
(
is_integer
(
$scope
))
{
$scope
=
$params
;
$class
=
$query
->
modelClass
;
$defaultScope
=
$class
::
defaultScope
();
if
(
$defaultScope
!==
null
)
{
call_user_func_array
(
$defaultScope
,
array
(
$query
));
}
$scopes
=
$class
::
scopes
();
foreach
(
$query
->
scopes
as
$name
=>
$params
)
{
if
(
is_integer
(
$name
))
{
$name
=
$params
;
$params
=
array
();
}
if
(
!
isset
(
$scopes
[
$name
]))
{
throw
new
Exception
(
"
$class
has no scope named '
$name
'."
);
}
array_unshift
(
$params
,
$query
);
call_user_func_array
(
$scope
,
$params
);
call_user_func_array
(
$scope
s
[
$name
]
,
$params
);
}
}
}
...
...
framework/db/ar/ActiveQuery.php
View file @
a53acdf6
...
...
@@ -89,6 +89,18 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
$this
->
modelClass
=
$modelClass
;
}
public
function
__call
(
$name
,
$params
)
{
$class
=
$this
->
modelClass
;
$scopes
=
$class
::
scopes
();
if
(
isset
(
$scopes
[
$name
]))
{
array_unshift
(
$params
,
$this
);
return
call_user_func_array
(
$scopes
[
$name
],
$params
);
}
else
{
return
parent
::
__call
(
$name
,
$params
);
}
}
public
function
asArray
(
$value
=
true
)
{
$this
->
asArray
=
$value
;
...
...
tests/unit/data/ar/Customer.php
View file @
a53acdf6
...
...
@@ -4,6 +4,9 @@ namespace yiiunit\data\ar;
class
Customer
extends
ActiveRecord
{
const
STATUS_ACTIVE
=
1
;
const
STATUS_INACTIVE
=
2
;
public
static
function
tableName
()
{
return
'tbl_customer'
;
...
...
@@ -17,4 +20,13 @@ class Customer extends ActiveRecord
),
);
}
public
static
function
scopes
()
{
return
array
(
'active'
=>
function
(
$q
)
{
return
$q
->
andWhere
(
'@.status = 1'
);
},
);
}
}
\ No newline at end of file
tests/unit/data/mysql.sql
View file @
a53acdf6
...
...
@@ -19,6 +19,7 @@ CREATE TABLE `tbl_customer` (
`email`
varchar
(
128
)
NOT
NULL
,
`name`
varchar
(
128
)
NOT
NULL
,
`address`
text
,
`status`
int
(
11
)
DEFAULT
0
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
...
...
@@ -71,9 +72,9 @@ CREATE TABLE `tbl_type` (
`bool_col2`
tinyint
(
1
)
DEFAULT
'1'
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
INSERT
INTO
tbl_customer
(
email
,
name
,
address
)
VALUES
(
'user1@example.com'
,
'user1'
,
'address1'
);
INSERT
INTO
tbl_customer
(
email
,
name
,
address
)
VALUES
(
'user2@example.com'
,
'user2'
,
'address2'
);
INSERT
INTO
tbl_customer
(
email
,
name
,
address
)
VALUES
(
'user3@example.com'
,
'user3'
,
'address3'
);
INSERT
INTO
tbl_customer
(
email
,
name
,
address
,
status
)
VALUES
(
'user1@example.com'
,
'user1'
,
'address1'
,
1
);
INSERT
INTO
tbl_customer
(
email
,
name
,
address
,
status
)
VALUES
(
'user2@example.com'
,
'user2'
,
'address2'
,
1
);
INSERT
INTO
tbl_customer
(
email
,
name
,
address
,
status
)
VALUES
(
'user3@example.com'
,
'user3'
,
'address3'
,
2
);
INSERT
INTO
tbl_category
(
name
)
VALUES
(
'Books'
);
INSERT
INTO
tbl_category
(
name
)
VALUES
(
'Movies'
);
...
...
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
a53acdf6
...
...
@@ -158,7 +158,12 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// find count
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
(
true
));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
count
());
$this
->
assertEquals
(
1
,
Customer
::
count
(
2
));
$this
->
assertEquals
(
2
,
Customer
::
count
(
array
(
'where'
=>
'id=1 OR id=2'
,
)));
}
public
function
testFindBySql
()
...
...
@@ -199,6 +204,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
3
,
$customer
->
id
);
$this
->
assertEquals
(
null
,
$customer
->
name
);
// scopes
$customers
=
Customer
::
find
()
->
active
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$customers
));
}
public
function
testEagerLoading
()
...
...
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