Skip to content

Commit

Permalink
improved guide about custom Query classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Sep 5, 2016
1 parent ea03f56 commit 5bdca12
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions docs/guide/db-active-record.md
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ in an Active Record class, you should override the [[yii\db\ActiveRecord::find()
of your customized query class. For example,

```php
// file Comment.php
namespace app\models;

use yii\db\ActiveRecord;
Expand All @@ -1350,18 +1351,18 @@ class Comment extends ActiveRecord
}
}

// file CommentQuery.php
namespace app\models;

class CommentQuery extends ActiveQuery
{
// ...
// ... add customized query methods here ...
}
```

Now whenever you are performing a query (e.g. `find()`, `findOne()`) or defining a relation (e.g. `hasOne()`)
with `Comment`, you will be working with an instance of `CommentQuery` instead of `ActiveQuery`.

> Tip: In big projects, it is recommended that you use customized query classes to hold most query-related code
so that the Active Record classes can be kept clean.

You can customize a query class in many creative ways to improve your query building experience. For example,
you can define new query building methods in a customized query class:

Expand All @@ -1375,6 +1376,9 @@ class CommentQuery extends ActiveQuery
}
```

> Tip: In big projects, it is recommended that you use customized query classes to hold most query-related code
so that the Active Record classes can be kept clean.

> Note: Instead of calling [[yii\db\ActiveQuery::where()|where()]], you usually should call
[[yii\db\ActiveQuery::andWhere()|andWhere()]] or [[yii\db\ActiveQuery::orWhere()|orWhere()]] to append additional
conditions when defining new query building methods so that any existing conditions are not overwritten.
Expand Down

0 comments on commit 5bdca12

Please sign in to comment.