Download the PHP package carono/yii2-attribute-behavior without Composer
On this page you can find all versions of the php package carono/yii2-attribute-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download carono/yii2-attribute-behavior
More information about carono/yii2-attribute-behavior
Files in carono/yii2-attribute-behavior
Download carono/yii2-attribute-behavior
More information about carono/yii2-attribute-behavior
Files in carono/yii2-attribute-behavior
Vendor carono
Package yii2-attribute-behavior
Short Description Yii2 behavior that triggers custom events when specific model attributes change. Provides flexible attribute-level event handling for ActiveRecord models with onChange{Attribute} and onInsert{Attribute} method patterns.
License MIT
Package yii2-attribute-behavior
Short Description Yii2 behavior that triggers custom events when specific model attributes change. Provides flexible attribute-level event handling for ActiveRecord models with onChange{Attribute} and onInsert{Attribute} method patterns.
License MIT
Please rate this library. Is it a good library?
Informations about the package yii2-attribute-behavior
Here’s a README in English for your Yii2 behavior code:
Model Attribute Event Behavior for Yii2
A behavior for Yii2 ActiveRecord that triggers custom methods when specific model attributes are changed. Useful for handling attribute-specific logic after insert/update operations.
Installation
Place the ModelAttributeEventBehaviour class in your project (e.g., under components/behaviors). Ensure the namespace matches your project structure.
Usage
- Extend the Behavior: Create a new class inheriting from
ModelAttributeEventBehaviour. - Attach to Model: Attach the behavior to your ActiveRecord model.
- Define Handlers: Implement methods in the format
onChange{AttributeName}oronInsert{AttributeName}to handle attribute changes.
Example
Step 1: Create a Custom Behavior Class
Step 2: Attach to Your Model
Method Parameters
Handler methods receive the following arguments:
$event: The AfterSaveEvent object.$insert: Boolean indicating if the model was just inserted.$model: The ActiveRecord model instance.$value: New value of the attribute.$oldValue: Old value of the attribute.$changedAttributes: Array of all changed attributes and their old values (as in$event->changedAttributes).
Events
- After Insert: Triggers methods prefixed with
onInsert(e.g.,onInsertEmailfor theemailattribute). - After Update: Triggers methods prefixed with
onChange(e.g.,onChangeStatusIdfor thestatus_idattribute).
Important Notes
- The
$changedAttributesarray contains the old values of changed attributes. - Handlers are only triggered if the attribute value actually changes (as detected by Yii2’s dirty attributes).
- The behavior automatically binds to the
EVENT_AFTER_INSERTandEVENT_AFTER_UPDATEevents.
License
MIT
All versions of yii2-attribute-behavior with dependencies
PHP Build Version
Package Version
The package carono/yii2-attribute-behavior contains the following files
Loading the files please wait ...