mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-12 01:11:23 +08:00
fixed docs about AR scopes
This commit is contained in:
@@ -703,9 +703,9 @@ Important points are:
|
|||||||
|
|
||||||
1. Class should extend from `yii\db\ActiveQuery` (or another `ActiveQuery` such as `yii\mongodb\ActiveQuery`).
|
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.
|
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:
|
For the example above, you need to write the following code:
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -715,9 +715,10 @@ use yii\db\ActiveRecord;
|
|||||||
|
|
||||||
class Comment extends 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
|
```php
|
||||||
class Post extends \yii\db\ActiveRecord
|
class Post extends \yii\db\ActiveRecord
|
||||||
{
|
{
|
||||||
public function getComments()
|
public function getActiveComments()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Comment::className(), ['post_id' => 'id'])->active();
|
return $this->hasMany(Comment::className(), ['post_id' => 'id'])->active();
|
||||||
|
|
||||||
@@ -752,20 +753,6 @@ $posts = Post::find()->with([
|
|||||||
])->all();
|
])->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
|
### 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
|
Transactional operations
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
|
||||||
When a few DB operations are related and are executed
|
When a few DB operations are related and are executed
|
||||||
|
|
||||||
TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
|
TODO: FIXME: WIP, TBD, https://github.com/yiisoft/yii2/issues/226
|
||||||
|
|||||||
Reference in New Issue
Block a user