-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Closed
Description
Pros:
- One proper way to define scopes.
- Thinner models.
- Finder is properly separated from model.
- Faster, no magic.
- Proper IDE autocomplete.
- Better API for declaring scopes, solves Scope declaration: remove $query from method signature? #2013
Cons:
- In order to use custom scopes you need ActiveQuery class for a model.
- Not familiar to 1.1 users.
Examples:
class UserQuery extends ActiveQuery
{
public function active()
{
$this->andWhere(['active' => 1]);
return $this;
}
public function age($years)
{
$this->andWhere(['age' => $years]);
return $this;
}
}
class User extends ActiveRecord
{
public static function createQuery()
{
return new UserQuery(['modelClass' => get_called_class()]);
}
}
$users = User::find()->active()->age(25)->all();