kak / behaviors
behaviors collection for Yii2
Installs: 1 219
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-10-28 22:29:39 UTC
README
ManyToManyBehavior (this project fork https://github.com/voskobovich/ManyToManyBehavior) MaterializedPathBehavior (this project fork https://github.com/matperez/yii2-materialized-path)
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist kak/behavior "*"
or add
"kak/behavior": "*"
to the require section of your composer.json
file.
Usage ManyToManyBehavior
#MaterializedPathBehavior
insert the following code to your ActiveRecord class:
use kak\models\behaviors\MaterializedPathBehavior // ... // step 1 public function behaviors() { return [ [ 'class' => MaterializedPathBehavior::class ], ]; } // step 2 /** * Query factory * @return MaterializedPathQuery */ public static function find() { return new MaterializedPathQuery(get_called_class()); }
Create class MaterializedPathQuery inherited from ActiveQuery
/** Class MaterializedPathQuery * @return bool */ class MaterializedPathQuery extends ActiveQuery { /** * Get root nodes * @return MaterializedPathModel */ public function getChildren($path) { /** @var ActiveQuery $this */ $this->andWhere(['path' => '.'.$path.'%']); return $this; } }
Set materialized path
$categoryModel = new Category(['parent_id' => 123 ]); $categoryModel->save();
#SluggableBehavior
insert the following code to your ActiveRecord class:
use kak\models\behaviors\SluggableBehavior; public function behaviors() { return [ [ 'class' => SluggableBehavior::class, 'attribute' => 'title', 'slugAttribute' => 'slug', // 'replacements' => [ '_' => '-'] // 'limit' => '100' // truncate text // 'delimiter' => '-' // default delimiter ] ]; }
#CallbackBehavior
insert the following code to your ActiveRecord class:
use kak\models\behaviors\CallbackBehavior; // ... public function behaviors() { return [ 'class' => CallbackBehavior::class, 'params' => [ 'operators' => [ 'attribute' => 'operator_list', // implode/explode method 'method' => CallbackBehavior::METHOD_STRING, 'delimiter' => '|', // set other delimiter only METHOD_STRING // or json_encode/json_decode method 'method' => CallbackBehavior::METHOD_JSON, // or custom callback 'set' => function ($value) { return implode(',', $value); }, 'get' => function ($value) { return explode(',', $value); }, ], ], ]; }
#UUIDBehavior
- is use postgress set actived module
uuid-ossp
create extension if not exists "uuid-ossp"
insert the following code to your ActiveRecord class:
use kak\models\behaviors\UUIDBehavior; // ... public function behaviors() { return [ [ 'class' => UUIDBehavior::class, 'createViaDb' => true // is use postgress ], ; }