PHPackages                             carono/yii2-attribute-behavior - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. carono/yii2-attribute-behavior

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

carono/yii2-attribute-behavior
==============================

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.

1.0.1(7mo ago)04MITPHPPHP ^7.4 || 8.\*

Since Sep 15Pushed 7mo agoCompare

[ Source](https://github.com/carono/carono-yii2-attribute-behavior)[ Packagist](https://packagist.org/packages/carono/yii2-attribute-behavior)[ RSS](/packages/carono-yii2-attribute-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Here’s a README in English for your Yii2 behavior code:

---

Model Attribute Event Behavior for Yii2
=======================================

[](#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
------------

[](#installation)

Place the `ModelAttributeEventBehaviour` class in your project (e.g., under `components/behaviors`). Ensure the namespace matches your project structure.

Usage
-----

[](#usage)

1. **Extend the Behavior**: Create a new class inheriting from `ModelAttributeEventBehaviour`.
2. **Attach to Model**: Attach the behavior to your ActiveRecord model.
3. **Define Handlers**: Implement methods in the format `onChange{AttributeName}` or `onInsert{AttributeName}` to handle attribute changes.

### Example

[](#example)

#### Step 1: Create a Custom Behavior Class

[](#step-1-create-a-custom-behavior-class)

```
namespace app\components\behaviors;

class MyEventBehavior extends \carono\yii2\behaviors\ModelAttributeEventBehaviour
{
    public function onChangeStatusId($event, $insert, $model, $value, $oldValue, $changedAttributes)
    {
        // Triggered when `status_id` changes
        if (!$insert && $oldValue != $value) {
            // Custom logic here (e.g., send notification, log change)
        }
    }

    public function onInsertEmail($event, $insert, $model, $value, $oldValue, $changedAttributes)
    {
        // Triggered when `email` is set during model creation
        if ($insert) {
            // Custom logic for new email
        }
    }
}
```

#### Step 2: Attach to Your Model

[](#step-2-attach-to-your-model)

```
namespace app\models;

use yii\db\ActiveRecord;
use app\components\behaviors\MyEventBehavior;

class User extends ActiveRecord
{
    public function behaviors()
    {
        return [
            MyEventBehavior::class,
        ];
    }
}
```

Method Parameters
-----------------

[](#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
------

[](#events)

- **After Insert**: Triggers methods prefixed with `onInsert` (e.g., `onInsertEmail` for the `email` attribute).
- **After Update**: Triggers methods prefixed with `onChange` (e.g., `onChangeStatusId` for the `status_id` attribute).

Important Notes
---------------

[](#important-notes)

- The `$changedAttributes` array 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_INSERT` and `EVENT_AFTER_UPDATE` events.

License
-------

[](#license)

MIT

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance63

Regular maintenance activity

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 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.

###  Release Activity

Cadence

Every ~13 days

Total

2

Last Release

226d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6193985?v=4)[Александр Касьянов](/maintainers/carono)[@carono](https://github.com/carono)

---

Top Contributors

[![carono](https://avatars.githubusercontent.com/u/6193985?v=4)](https://github.com/carono "carono (3 commits)")

---

Tags

yii2extension

### Embed Badge

![Health badge](/badges/carono-yii2-attribute-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/carono-yii2-attribute-behavior/health.svg)](https://phpackages.com/packages/carono-yii2-attribute-behavior)
```

###  Alternatives

[vyants/yii2-daemon

Extension provides functionality for simple daemons creation and control

7859.0k](/packages/vyants-yii2-daemon)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

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