PHP code example of illusiard / yii2-mass-event-layer

1. Go to this page and download the library: Download illusiard/yii2-mass-event-layer 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/ */

    

illusiard / yii2-mass-event-layer example snippets


'components' => [
    'db' => [
        'class' => illusiard\massEvents\components\db\Connection::class,
        'dsn' => 'mysql:host=localhost;dbname=app',
        'username' => 'user',
        'password' => 'secret',
        'charset' => 'utf8',
    ],
],

use yii\base\Event;
use illusiard\massEvents\components\MassEventLayer;

Event::on(MassEventLayer::class, 'db.updateAll', function ($e) {
    /** @var \illusiard\massEvents\components\MassEventYiiEvent $e */
    $payload = $e->payload;

    // $payload example:
    // [
    //   'name' => 'db.updateAll',
    //   'occurredAt' => 1700000000,
    //   'payload' => [
    //     'db' => 'mysql:host=...;dbname=...',
    //     'table' => 'employee',
    //     'operation' => 'updateAll',
    //     'affectedRows' => 42,
    //     'set' => ['status' => 'archived'],
    //     'condition' => ['department_id' => 10],
    //     'params' => [],
    //   ],
    //   'meta' => ['captured' => true],
    // ]
});

Event::on(MassEventLayer::class, MassEventLayer::EVENT_ANY, function ($e) {
    /** @var \illusiard\massEvents\components\MassEventYiiEvent $e */
    // handle any mass event
});

use illusiard\massEvents\components\filters\IgnoreTablesFilter;
use illusiard\massEvents\components\filters\IgnoreSqlPatternsFilter;
use illusiard\massEvents\components\filters\SamplingFilter;

Yii::$app->massEvents->setFilters([
    new IgnoreTablesFilter(['migration', 'cache']),
    new IgnoreSqlPatternsFilter(['/^\s*SELECT\b/i']),
    new SamplingFilter(0.25),
]);

use illusiard\massEvents\components\filters\IgnoreTablesFilter;

Yii::$app->massEvents->addFilter(new IgnoreTablesFilter(['except' => ['migration', 'cache', 'log_*']]));
Yii::$app->massEvents->addFilter(new IgnoreTablesFilter(['only' => ['employee', 'department']]));

use illusiard\massEvents\components\filters\IgnoreSqlPatternsFilter;

Yii::$app->massEvents->addFilter(new IgnoreSqlPatternsFilter(['except' => ['/^\s*SELECT\b/i']]));
Yii::$app->massEvents->addFilter(new IgnoreSqlPatternsFilter(['only' => ['/^\s*UPDATE\b/i']]));

Yii::$app->massEvents->setSerializer(new MySerializer());