Browse Source

updated doc about using scopes.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
65d72eb75b
  1. 31
      docs/guide/active-record.md

31
docs/guide/active-record.md

@ -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: via [[find()]] or [[findBySql()]]. The following is an example:
```php ```php
class Customer extends \yii\db\ActiveRecord class Comment 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 Or use the scopes on-the-fly when performing relational query:
through `ActiveQuery` returned by `Customer::find()`.
```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: Scopes can be parameterized. For example, we can define and use the following `olderThan` scope:

Loading…
Cancel
Save