PHPackages                             mootensai/yii2-before-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mootensai/yii2-before-query

ActiveLibrary[Database &amp; ORM](/categories/database)

mootensai/yii2-before-query
===========================

Add before query or before find event on Yii 2 models

5971[1 issues](https://github.com/mootensai/yii2-before-query/issues)PHP

Since Jun 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/mootensai/yii2-before-query)[ Packagist](https://packagist.org/packages/mootensai/yii2-before-query)[ RSS](/packages/mootensai-yii2-before-query/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (1)Used By (0)

yii2-before-query
=================

[](#yii2-before-query)

Add before query event on Yii 2 models

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require mootensai/yii2-before-query "dev-master"
```

or add

```
"mootensai/yii2-before-query": "dev-master"

```

to the `require` section of your `composer.json` file.

1. Base Trait Before Query

---

```
namespace common\traits\base;
trait BeforeQueryTrait{

    public static function find() {
        $obj = new static;
        $class = new \ReflectionClass($obj);
        $condition = [];
        foreach ($class->getProperties(\ReflectionProperty::IS_STATIC) as $property) {
            if(strpos($property->getName(),'BEFORE_QUERY') !== false && is_array($property->getValue($obj))){
                $condition = array_merge($condition, $property->getValue($obj));
            }
        }
        return parent::find()->andFilterWhere($condition);
    }
}
```

2. Add new property on model

---

Next, you can add new property on your model like this :

```
class MyClass extends \yii\db\ActiveRecord{
    use \common\traits\base\BeforeQueryTrait;
    public static $BEFORE_QUERY = ['myColumn' => 'myValue'];
}
```

NB : the content of your `public static $BEFORE_QUERY` can be learn here

3. You can create a new trait.

---

For example, i've created Soft Delete Boolean Trait :

```
trait SoftDeleteBoolTrait{
    public static $BEFORE_QUERY_SOFT_DELETE = ['isdeleted' => 0];

    public function deleteSoft() {
        $col = key(static::$BEFORE_QUERY_SOFT_DELETE);
        $this->{$col} = 1;
        return $this->save(false,[$col]);
    }

    public static function restore($id) {
        $col = key(static::$BEFORE_QUERY_SOFT_DELETE);
        $model = parent::findOne($id, 1);
        $model->{$col} = 0;
        $model->save(false,[$col]);
    }
}
```

Use it on model :

```
class MyClass extends \yii\db\ActiveRecord{
    use \mootensai\beforequery\base\BeforeQueryTrait;
    use \mootensai\beforequery\traits\SoftDeleteBoolTrait;
}
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6abdf40661b0e9171dce645d1cfd2c9f8fa8547fc0549ee65338b788435650aa?d=identicon)[mootensai](/maintainers/mootensai)

---

Top Contributors

[![mootensai](https://avatars.githubusercontent.com/u/5844149?v=4)](https://github.com/mootensai "mootensai (46 commits)")

### Embed Badge

![Health badge](/badges/mootensai-yii2-before-query/health.svg)

```
[![Health](https://phpackages.com/badges/mootensai-yii2-before-query/health.svg)](https://phpackages.com/packages/mootensai-yii2-before-query)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198741.5k12](/packages/pgvector-pgvector)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
