PHP code example of carono / yii2-attribute-behavior

1. Go to this page and download the library: Download carono/yii2-attribute-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carono / yii2-attribute-behavior example snippets


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
        }
    }
}

namespace app\models;

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

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