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
8c268939
Commit
8c268939
authored
Aug 09, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented new scope declaration syntax.
parent
ea30da6a
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
31 deletions
+25
-31
ActiveFinder.php
framework/db/ar/ActiveFinder.php
+7
-4
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+3
-5
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+2
-4
Customer.php
tests/unit/data/ar/Customer.php
+3
-7
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+10
-11
No files found.
framework/db/ar/ActiveFinder.php
View file @
8c268939
...
@@ -196,6 +196,7 @@ class ActiveFinder extends \yii\base\Object
...
@@ -196,6 +196,7 @@ class ActiveFinder extends \yii\base\Object
$records
[
$row
[
$query
->
index
]]
=
$row
;
$records
[
$row
[
$query
->
index
]]
=
$row
;
}
}
}
else
{
}
else
{
/** @var $class ActiveRecord */
$class
=
$query
->
modelClass
;
$class
=
$query
->
modelClass
;
if
(
$query
->
index
===
null
)
{
if
(
$query
->
index
===
null
)
{
foreach
(
$rows
as
$row
)
{
foreach
(
$rows
as
$row
)
{
...
@@ -212,18 +213,20 @@ class ActiveFinder extends \yii\base\Object
...
@@ -212,18 +213,20 @@ class ActiveFinder extends \yii\base\Object
protected
function
applyScopes
(
$query
)
protected
function
applyScopes
(
$query
)
{
{
/** @var $class ActiveRecord */
$class
=
$query
->
modelClass
;
$class
=
$query
->
modelClass
;
$class
::
defaultScope
(
$query
);
$class
::
defaultScope
(
$query
);
if
(
is_array
(
$query
->
scopes
))
{
if
(
is_array
(
$query
->
scopes
))
{
$
scopes
=
$class
::
scopes
()
;
$
model
=
new
$class
;
foreach
(
$query
->
scopes
as
$name
=>
$params
)
{
foreach
(
$query
->
scopes
as
$name
=>
$params
)
{
if
(
is_integer
(
$name
))
{
if
(
is_string
(
$params
))
{
// scope name only without parameters
$name
=
$params
;
$name
=
$params
;
$params
=
array
();
$params
=
array
();
}
}
if
(
isset
(
$scopes
[
$name
]
))
{
if
(
method_exists
(
$class
,
$name
))
{
array_unshift
(
$params
,
$query
);
array_unshift
(
$params
,
$query
);
call_user_func_array
(
$scopes
[
$name
]
,
$params
);
call_user_func_array
(
array
(
$model
,
$name
)
,
$params
);
}
else
{
}
else
{
throw
new
Exception
(
"
$class
has no scope named '
$name
'."
);
throw
new
Exception
(
"
$class
has no scope named '
$name
'."
);
}
}
...
...
framework/db/ar/ActiveQuery.php
View file @
8c268939
...
@@ -46,11 +46,9 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
...
@@ -46,11 +46,9 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
public
function
__call
(
$name
,
$params
)
public
function
__call
(
$name
,
$params
)
{
{
$class
=
$this
->
modelClass
;
if
(
method_exists
(
$this
->
modelClass
,
$name
))
{
$scopes
=
$class
::
scopes
();
$this
->
scopes
[
$name
]
=
$params
;
if
(
isset
(
$scopes
[
$name
]))
{
return
$this
;
array_unshift
(
$params
,
$this
);
return
call_user_func_array
(
$scopes
[
$name
],
$params
);
}
else
{
}
else
{
return
parent
::
__call
(
$name
,
$params
);
return
parent
::
__call
(
$name
,
$params
);
}
}
...
...
framework/db/ar/ActiveRecord.php
View file @
8c268939
...
@@ -164,10 +164,8 @@ abstract class ActiveRecord extends Model
...
@@ -164,10 +164,8 @@ abstract class ActiveRecord extends Model
* echo Customer::count('COUNT(DISTINCT age)')->value();
* echo Customer::count('COUNT(DISTINCT age)')->value();
* ~~~
* ~~~
*
*
* @param mixed $q the query parameter. This can be one of the followings:
* @param array $q the query configuration. This should be an array of name-value pairs.
*
* It will be used to configure the [[ActiveQuery]] object for query purpose.
* - a scalar value (integer or string): query by a single primary key value.
* - an array of name-value pairs: it will be used to configure the [[ActiveQuery]] object for query purpose.
*
*
* @return integer the counting result
* @return integer the counting result
*/
*/
...
...
tests/unit/data/ar/Customer.php
View file @
8c268939
...
@@ -21,13 +21,8 @@ class Customer extends ActiveRecord
...
@@ -21,13 +21,8 @@ class Customer extends ActiveRecord
);
);
}
}
public
static
function
scopes
(
)
public
function
active
(
$query
)
{
{
$status
=
self
::
STATUS_ACTIVE
;
return
$query
->
andWhere
(
'@.`status` = '
.
self
::
STATUS_ACTIVE
);
return
array
(
'active'
=>
function
(
$q
)
use
(
$status
){
return
$q
->
andWhere
(
'@.`status` = '
.
$status
);
},
);
}
}
}
}
\ No newline at end of file
tests/unit/framework/db/ar/ActiveRecordTest.php
View file @
8c268939
...
@@ -35,7 +35,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -35,7 +35,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
public
function
testUpdate
()
public
function
testUpdate
()
{
{
// save
// save
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
...
@@ -43,7 +43,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -43,7 +43,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$customer
->
save
();
$customer
->
save
();
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2x'
,
$customer
->
name
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$customer2
=
Customer
::
find
(
2
)
->
one
()
;
$customer2
=
Customer
::
find
(
2
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
$this
->
assertEquals
(
'user2x'
,
$customer2
->
name
);
// updateCounters
// updateCounters
...
@@ -57,13 +57,13 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -57,13 +57,13 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
$this
->
assertEquals
(
0
,
$orderItem
->
quantity
);
// updateAll
// updateAll
$customer
=
Customer
::
find
(
3
)
->
one
()
;
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$ret
=
Customer
::
updateAll
(
array
(
$ret
=
Customer
::
updateAll
(
array
(
'name'
=>
'temp'
,
'name'
=>
'temp'
,
),
array
(
'id'
=>
3
));
),
array
(
'id'
=>
3
));
$this
->
assertEquals
(
1
,
$ret
);
$this
->
assertEquals
(
1
,
$ret
);
$customer
=
Customer
::
find
(
3
)
->
one
()
;
$customer
=
Customer
::
find
(
3
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
$this
->
assertEquals
(
'temp'
,
$customer
->
name
);
// updateCounters
// updateCounters
...
@@ -81,11 +81,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -81,11 +81,11 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
public
function
testDelete
()
public
function
testDelete
()
{
{
// delete
// delete
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$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
->
delete
();
$customer
->
delete
();
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$customer
=
Customer
::
find
(
2
);
$this
->
assertNull
(
$customer
);
$this
->
assertNull
(
$customer
);
// deleteAll
// deleteAll
...
@@ -140,7 +140,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -140,7 +140,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertTrue
(
$result
[
2
]
instanceof
Customer
);
$this
->
assertTrue
(
$result
[
2
]
instanceof
Customer
);
// find by a single primary key
// find by a single primary key
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$customer
=
Customer
::
find
(
2
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
...
@@ -161,7 +161,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -161,7 +161,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
// find count
// find count
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
count
());
$this
->
assertEquals
(
3
,
Customer
::
count
());
$this
->
assertEquals
(
1
,
Customer
::
count
(
2
));
$this
->
assertEquals
(
2
,
Customer
::
count
(
array
(
$this
->
assertEquals
(
2
,
Customer
::
count
(
array
(
'where'
=>
'id=1 OR id=2'
,
'where'
=>
'id=1 OR id=2'
,
)));
)));
...
@@ -294,12 +293,12 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -294,12 +293,12 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
public
function
testLazyLoading
()
public
function
testLazyLoading
()
{
{
// has one
// has one
$order
=
Order
::
find
(
3
)
->
one
()
;
$order
=
Order
::
find
(
3
);
$this
->
assertTrue
(
$order
->
customer
instanceof
Customer
);
$this
->
assertTrue
(
$order
->
customer
instanceof
Customer
);
$this
->
assertEquals
(
2
,
$order
->
customer
->
id
);
$this
->
assertEquals
(
2
,
$order
->
customer
->
id
);
// has many
// has many
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$customer
=
Customer
::
find
(
2
);
$orders
=
$customer
->
orders
;
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
0
]
->
id
);
...
@@ -316,7 +315,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
...
@@ -316,7 +315,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this
->
assertEquals
(
2
,
$orders
[
2
]
->
books
[
0
]
->
id
);
$this
->
assertEquals
(
2
,
$orders
[
2
]
->
books
[
0
]
->
id
);
// customized relation query
// customized relation query
$customer
=
Customer
::
find
(
2
)
->
one
()
;
$customer
=
Customer
::
find
(
2
);
$orders
=
$customer
->
orders
(
array
(
$orders
=
$customer
->
orders
(
array
(
'where'
=>
'@.id = 3'
,
'where'
=>
'@.id = 3'
,
));
));
...
...
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