All Projects → yii2tech → behavior-trait

yii2tech / behavior-trait

Licence: other
Allows handling events via inline declared methods, which can be added by traits

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to behavior-trait

install
basic script for project installation
Stars: ✭ 17 (-5.56%)
Mutual labels:  yii2, yii, yii2-extension
Ar Position
ActiveRecord behavior, which provides ability for custom records order setup
Stars: ✭ 107 (+494.44%)
Mutual labels:  yii2, yii, yii2-extension
Ar Linkmany
ActiveRecord behavior for saving many-to-many relations
Stars: ✭ 83 (+361.11%)
Mutual labels:  yii2, yii, yii2-extension
yii2-db
Database extensions for Yii 2.0 Framework 📦
Stars: ✭ 19 (+5.56%)
Mutual labels:  behavior, yii2, trait
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+844.44%)
Mutual labels:  yii2, yii, yii2-extension
Csv Grid
Yii2 extension for CSV export
Stars: ✭ 83 (+361.11%)
Mutual labels:  yii2, yii, yii2-extension
Admin
Admin pack (actions, widgets, etc) for Yii2
Stars: ✭ 100 (+455.56%)
Mutual labels:  yii2, yii, yii2-extension
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (+16.67%)
Mutual labels:  yii2, yii, yii2-extension
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+800%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Assets Auto Compress
Automatic compilation of js + css + html
Stars: ✭ 147 (+716.67%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Schemadump
Generate the schema from an existing database.
Stars: ✭ 78 (+333.33%)
Mutual labels:  yii2, yii, yii2-extension
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (+72.22%)
Mutual labels:  yii2, yii, yii2-extension
Sitemap
Site map creation support
Stars: ✭ 59 (+227.78%)
Mutual labels:  yii2, yii, yii2-extension
yii2-linkable-behavior
Yii2 behavior to help creating urls easier
Stars: ✭ 12 (-33.33%)
Mutual labels:  behavior, yii2, yii2-extension
Config
Yii2 application runtime configuration support
Stars: ✭ 54 (+200%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Aws S3
An Amazon S3 component for Yii2
Stars: ✭ 86 (+377.78%)
Mutual labels:  yii2, yii, yii2-extension
ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (+138.89%)
Mutual labels:  yii2, yii, yii2-extension
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+300%)
Mutual labels:  yii2, yii, yii2-extension
File Storage
File storage abstraction for Yii2
Stars: ✭ 116 (+544.44%)
Mutual labels:  yii2, yii, yii2-extension
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+944.44%)
Mutual labels:  yii2, yii, yii2-extension

Behavior Trait Extension for Yii 2


This extension provides the ability of handling events via inline declared methods, which can be added via traits.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2tech/behavior-trait

or add

"yii2tech/behavior-trait": "*"

to the require section of your composer.json.

Usage

This extension introduces special trait [[\yii2tech\behaviortrait\BehaviorTrait]], which if used provides the ability of handling events via inline declared methods, which can be added via other traits. This trait can be added to any descendant of [[\yii\base\Component]].

Each event handler method should be named by pattern: '{eventName}Handler{UniqueSuffix}', where 'eventName' is a name of the event the method should handle, 'UniqueSuffix' any suffix, which separate particular event handler method from the others. For example: if the class has an event 'beforeInsert' it can introduce method named beforeInsertHandlerEncryptPassword, which will be automatically triggered when event 'beforeInsert' is triggered:

use yii\db\ActiveRecord;
use yii2tech\behaviortrait\BehaviorTrait;

class User extends ActiveRecord
{
    use BehaviorTrait; // add `BehaviorTrait` allowing to use inline event handlers
    use EncryptPasswordTrait; // add trait, which introduce inline event handler

    // ...
}

trait EncryptPasswordTrait
{
    public function beforeInsertHandlerEncryptPassword(Event $event)
    {
        if (!empty($this->newPassword)) {
            $this->password = sha1($this->newPassword);
        }
    }
}

Attention: watch for the naming collisions, ensure any inline handler declared either in class or via trait has a unique name (with unique suffix)!

Note: using traits instead behaviors improves performance but is less flexible. Keep in mind that such approach has been rejected at Yii2 core at yiisoft/yii2#1041.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].