PHP code example of rasuvaeff / yii3-workflow-db

1. Go to this page and download the library: Download rasuvaeff/yii3-workflow-db 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/ */

    

rasuvaeff / yii3-workflow-db example snippets


// config/common/di/migration.php
use Yiisoft\Db\Migration\Service\MigrationService;

return [
    MigrationService::class => [
        'setSourceNamespaces()' => [['App\\Migration', 'Rasuvaeff\\Yii3WorkflowDb\\Migration']],
    ],
];

// config/common/params.php
return [
    'rasuvaeff/yii3-workflow-db' => [
        'table' => 'workflow_transitions',
        'table_prefix' => '',    // prepended to `table`; e.g. 'rsv_' → rsv_workflow_transitions
        'retentionDays' => 90,   // default for workflow:transitions:prune
    ],
];

$workflow = $registry->get('order');

if (!$workflow->applyOnce($order, 'ship', idempotencyKey: $requestId)) {
    return;   // replay: either the lookup or the unique index said so
}

use Rasuvaeff\Yii3WorkflowDb\DbTransitionLog;

$log->forSubject('order', $order->getId());   // chronological
$log->latest('order', limit: 50, offset: 0);  // newest first, for an admin screen
$log->count('order');                         // total rows, for the pager
$log->prune(new DateTimeImmutable('-90 days'));

$this->db->transaction(function () use ($order, $requestId): void {
    if (!$this->registry->get('order')->applyOnce($order, 'ship', $requestId)) {
        return;
    }

    $this->orders->save($order);
});

use Rasuvaeff\Yii3WorkflowDb\WorkflowTransaction;

final readonly class ShipOrderHandler
{
    public function __construct(
        private WorkflowRegistry $registry,
        private WorkflowTransaction $transaction,
    ) {}

    public function handle(Order $order, string $requestId): void
    {
        $this->transaction->applyOnce(
            $this->registry->get('order'),
            $order,
            'ship',
            $requestId,
            then: fn() => $this->orders->save($order),
        );
    }
}