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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
65d72eb7
Commit
65d72eb7
authored
Dec 19, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated doc about using scopes.
parent
19c7c001
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
4 deletions
+27
-4
active-record.md
docs/guide/active-record.md
+27
-4
No files found.
docs/guide/active-record.md
View file @
65d72eb7
...
...
@@ -450,7 +450,7 @@ in the ActiveRecord classes. They can be invoked through the [[ActiveQuery]] obj
via
[
[find()
]
] or
[
[findBySql()
]
]. The following is an example:
```
php
class
C
ustomer
extends
\yii\db\ActiveRecord
class
C
omment
extends
\yii\db\ActiveRecord
{
// ...
...
...
@@ -463,11 +463,34 @@ class Customer extends \yii\db\ActiveRecord
}
}
$customers
=
Customer
::
find
()
->
active
()
->
all
();
$comments
=
Comment
::
find
()
->
active
()
->
all
();
```
In the above, the
`active()`
method is defined in
`Comment`
while we are calling it
through
`ActiveQuery`
returned by
`Comment::find()`
.
You can also use scopes when defining relations. For example,
```
php
class
Post
extends
\yii\db\ActiveRecord
{
public
function
getComments
()
{
return
$this
->
hasMany
(
Comment
::
className
(),
[
'post_id'
=>
'id'
])
->
active
();
}
}
```
In the above, the
`active()`
method is defined in
`Customer`
while we are calling it
through
`ActiveQuery`
returned by
`Customer::find()`
.
Or use the scopes on-the-fly when performing relational query:
```
php
$posts
=
Post
::
find
()
->
with
([
'comments'
=>
function
(
$q
)
{
$q
->
active
();
}
])
->
all
();
```
Scopes can be parameterized. For example, we can define and use the following
`olderThan`
scope:
...
...
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