https://github.com/mootensai/yii2-before-query
Add before query event on Yii 2 models
https://github.com/mootensai/yii2-before-query
Last synced: 17 days ago
JSON representation
Add before query event on Yii 2 models
- Host: GitHub
- URL: https://github.com/mootensai/yii2-before-query
- Owner: mootensai
- Created: 2015-05-02T03:32:25.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-18T11:21:57.000Z (almost 10 years ago)
- Last Synced: 2024-09-18T00:39:53.319Z (8 months ago)
- Language: PHP
- Size: 250 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yii2-before-query
Add before query event on Yii 2 models
## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
$ 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
------------------```php
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 :
```php
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 herehttp://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where%28%29-detail
3. You can create a new trait.
------------------------------------------------------------------------For example, i've created Soft Delete Boolean Trait :
```php
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 :
```php
class MyClass extends \yii\db\ActiveRecord{
use \mootensai\beforequery\base\BeforeQueryTrait;
use \mootensai\beforequery\traits\SoftDeleteBoolTrait;
}
```