matrozov/yii2-pro-active-query

Dynamically extensible ActiveQuery class

dev-master 2023-01-23 20:10 UTC

This package is auto-updated.

Last update: 2024-04-23 22:58:37 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.