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
cdb1221a
Commit
cdb1221a
authored
Feb 21, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed docs about AR scopes
parent
5ee50a84
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
20 deletions
+24
-20
active-record.md
docs/guide/active-record.md
+24
-20
No files found.
docs/guide/active-record.md
View file @
cdb1221a
...
...
@@ -703,9 +703,9 @@ Important points are:
1.
Class should extend from
`yii\db\ActiveQuery`
(or another
`ActiveQuery`
such as
`yii\mongodb\ActiveQuery`
).
2.
A method should be
`public`
and should return
`$this`
in order to allow method chaining. It may accept parameters.
3.
Check
`ActiveQuery`
methods that are very useful for modifying query conditions.
3.
Check
[
[yii\db\ActiveQuery
]
]
methods that are very useful for modifying query conditions.
Second, override
`ActiveRecord::createQuery()`
to use the custom query class instead of the regular
`ActiveQuery`
.
Second, override
[
[yii\db\ActiveRecord::createQuery()
]
] to use the custom query class instead of the regular
[
[yii\db\ActiveQuery|ActiveQuery
]
]
.
For the example above, you need to write the following code:
```
...
...
@@ -715,9 +715,10 @@ use yii\db\ActiveRecord;
class Comment extends ActiveRecord
{
public static function createQuery()
public static function createQuery(
$config = []
)
{
return new CommentQuery(['modelClass' => get_called_class()]);
$config['modelClass'] = get_called_class();
return new CommentQuery($config);
}
}
```
...
...
@@ -734,7 +735,7 @@ You can also use scopes when defining relations. For example,
```
php
class
Post
extends
\yii\db\ActiveRecord
{
public
function
getComments
()
public
function
get
Active
Comments
()
{
return
$this
->
hasMany
(
Comment
::
className
(),
[
'post_id'
=>
'id'
])
->
active
();
...
...
@@ -752,20 +753,6 @@ $posts = Post::find()->with([
])
->
all
();
```
### Default Scope
If you used Yii 1.1 before, you may know a concept called
*default scope*
. A default scope is a scope that
applies to ALL queries. You can define a default scope easily by overriding
`ActiveRecord::createQuery()`
. For example,
```
php
public
static
function
createQuery
()
{
$query
=
new
CommentQuery
([
'modelClass'
=>
get_called_class
()]);
$query
->
where
([
'deleted'
=>
false
]);
return
$query
;
}
```
### Making it IDE-friendly
...
...
@@ -794,10 +781,27 @@ class CommentQuery extends ActiveQuery
}
```
### Default Scope
If you used Yii 1.1 before, you may know a concept called
*default scope*
. A default scope is a scope that
applies to ALL queries. You can define a default scope easily by overriding
[
[yii\db\ActiveRecord::createQuery()
]
]. For example,
```
php
public
static
function
createQuery
(
$config
=
[])
{
$config
[
'modelClass'
]
=
get_called_class
();
return
(
new
ActiveQuery
(
$config
))
->
where
([
'deleted'
=>
false
]);
}
```
Note that all your queries should then not use
[
[yii\db\ActiveQuery::where()|where()
]
] but
[
[yii\db\ActiveQuery::andWhere()|andWhere()
]
] and
[
[yii\db\ActiveQuery::orWhere()|orWhere()
]
]
to not override the default condition.
Transactional operations
------------------------
When a few DB operations are related and are executed
TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
...
...
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