2017 © Pedro Peláez
 

library yii2-lifecycle-behavior

Define the lifecycle of a model by defining allowed satus changes in terms of a state machine.

image

cebe/yii2-lifecycle-behavior

Define the lifecycle of a model by defining allowed satus changes in terms of a state machine.

  • Wednesday, February 10, 2016
  • by cebe
  • Repository
  • 9 Watchers
  • 23 Stars
  • 4,629 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 8 % Grown

The README.md

Yii 2 lifecycle behavior

Define the lifecycle of a model by defining allowed status changes in terms of a state machine., (*1)

Latest Stable Version Total Downloads License Build Status, (*2)

Installation

This is an extension for the Yii 2 PHP framework., (*3)

Installation is recommended to be done via composer by running:, (*4)

composer require cebe/yii2-lifecycle-behavior

Alternatively you can add the following to the require section in your composer.json manually and run composer update afterwards:, (*5)

"cebe/yii2-lifecycle-behavior": "~2.0.0"

Usage

You can add the behavior to an ActiveRecord class. It does not work with yii\base\Model because it relies on the old-attribute feature which is only available in active record., (*6)

You can add the behavior to the model by creating a behaviors() method if there is none yet, or add it to the list of exising behaviors., (*7)

The following example shows how to define the allowed status changes:, (*8)

    public function behaviors()
    {
        return [
            'lifecycle' => [
                'class' => cebe\lifecycle\LifecycleBehavior::class,
                'validStatusChanges' => [
                    'draft'     => ['ready', 'delivered'],
                    'ready'     => ['draft', 'delivered'],
                    'delivered' => ['payed', 'archived'],
                    'payed'     => ['archived'],
                    'archived'  => [],
                ],
            ],
        ];
    }

The above state transitions can be visualized as the following state machine:, (*9)

Visualization of state transitions, (*10)

Status field validation

By default, the behavior will validate the status attribute of the record, when validate() or save() is called and add a validation error in case state has changed in a way that is not allowed., (*11)

  • The attribute to validate can be configured by setting the statusAttribute property of the behavior.
  • The error message can be configured by setting the validationErrorMessage property of the behavior. The place holders {old} and {new} are being replaced with the corresponding status values.

Program flow validation

The behavior may also be used to validate status changes in program flow. This is different to user input validation as described above, because program flow will be aborted by an exception in this case. For user input, the recipient of the error message is the user, when status is not changed by the user, the recipient of the error is the developer., (*12)

Configuring different validation methods

By default status field is validated both, on validation and on update. To disable one of the methods, you may configure the $events propery, which is by default:, (*13)

'events' => [
    BaseActiveRecord::EVENT_BEFORE_VALIDATE => 'handleBeforeValidate',
    BaseActiveRecord::EVENT_BEFORE_UPDATE => 'handleBeforeSave',
]

The Versions

10/02 2016

dev-master

9999999-dev https://github.com/cebe/markdown#readme

Define the lifecycle of a model by defining allowed satus changes in terms of a state machine.

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 validation model lifecycle

08/03 2015

1.0.0

1.0.0.0 https://github.com/cebe/markdown#readme

Define the lifecycle of a model by defining allowed satus changes.

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 validation model lifecycle