matrozov / yii2-pro-active-query
Dynamically extensible ActiveQuery class
Installs: 911
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:extension
Requires
- php: >=7.2
- yiisoft/yii2: ~2.0.14
This package is auto-updated.
Last update: 2025-03-24 00:48:32 UTC
README
This extension provides dynamical customization ActiveQuery filters.
For license information check the LICENSE-file.
Install
Either run
$ php composer.phar require matrozov/yii2-pro-active-query
or add
"matrozov/yii2-pro-active-query": "@dev"
to the require
section of your composer.json
file.
Usage
Simply add ProActiveQueryTrait
trait to your ActiveRecord class and specify ProActiveQuery query
-function like this:
class MyClass extends ActiveRecord { use ProActiveQueryTrait; ... public static function queryMyFunc(ActiveQuery &$query) { $query->andWhere(['deleted_at' => null']); } }
Now, you can simple selection from database with your filter function:
$items = MyClass::find()->myFunc()->all();
Any function with any parameters with query
-prefix can be called from ActiveQuery:
public static function queryStatusIs(ActiveQuery &$query, $status) { $query->andWhere(['status' => $status]); }
$items = MyClass::find()->statusIs('ready')->all();
You can use additional trait for share query
-function between ActiveRecord classes.