All Projects → cebe → Yii2 Lifecycle Behavior

cebe / Yii2 Lifecycle Behavior

Licence: mit
Define the lifecycle of a model by defining allowed status changes.

Projects that are alternatives of or similar to Yii2 Lifecycle Behavior

Yii2 Elfinder
elFinder file manager for Yii 2
Stars: ✭ 21 (-55.32%)
Mutual labels:  yii2, yii2-extension
Yii2 Image Attachment
This extension intended to handle images associated with model.
Stars: ✭ 35 (-25.53%)
Mutual labels:  yii2, yii2-extension
Yii2 Gentelella
Free admin template for backend
Stars: ✭ 266 (+465.96%)
Mutual labels:  yii2, yii2-extension
Yii2 Gravatar
Gravatar Widget for Yii Framework 2
Stars: ✭ 36 (-23.4%)
Mutual labels:  yii2, yii2-extension
Yii2 C3 Chart
Yii2 wrapper for D3-based reusable chart library
Stars: ✭ 9 (-80.85%)
Mutual labels:  yii2, yii2-extension
Yii2 Cms
Simple CMS extension
Stars: ✭ 42 (-10.64%)
Mutual labels:  yii2, yii2-extension
Yii2 Google Maps Markers
Google Maps Markers Widget for Yii2
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
yii2-jwt-user
JWT (JSON Web Token) User component for Yii 2
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
Yii2 Slack Log
Pretty Slack log target for Yii 2
Stars: ✭ 24 (-48.94%)
Mutual labels:  yii2, yii2-extension
Yii2 Telegram Log
Telegram log target for Yii 2
Stars: ✭ 24 (-48.94%)
Mutual labels:  yii2, yii2-extension
yii2-queuemanager
Yii2 Queue Manager (Analytic & Monitor)
Stars: ✭ 18 (-61.7%)
Mutual labels:  yii2, yii2-extension
Yii2 Relation Trait
Yii 2 Models add functionality for load with relation, & transactional save with relation PLUS soft delete/restore feature
Stars: ✭ 47 (+0%)
Mutual labels:  yii2, yii2-extension
collection
Basic collection library for Yii Framework 2.0
Stars: ✭ 29 (-38.3%)
Mutual labels:  yii2, yii2-extension
Yii2 User
[ABANDONED] Flexible user registration and authentication module for Yii2
Stars: ✭ 946 (+1912.77%)
Mutual labels:  yii2, yii2-extension
yii2-blog
Simple, configurable blog module for Yii2 (post, comment, nested category, tags). + frontend, backend. + SEO! (Opengraph, Schema.org) ~~~COMING SOON V2.0~~~ Please STAR this repo
Stars: ✭ 79 (+68.09%)
Mutual labels:  yii2, yii2-extension
Yii2 Helpers
Collection of useful helper functions for Yii Framework 2.0
Stars: ✭ 16 (-65.96%)
Mutual labels:  yii2, yii2-extension
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (-55.32%)
Mutual labels:  yii2, yii2-extension
yii2-firebird
Firebird connector for Yii2 framework
Stars: ✭ 23 (-51.06%)
Mutual labels:  yii2, yii2-extension
Yii2 Selectize
selectize.js wrapper for yii2.
Stars: ✭ 18 (-61.7%)
Mutual labels:  yii2, yii2-extension
Yii2 Bx Slider
bx-slider.js wrapper for yii2.
Stars: ✭ 11 (-76.6%)
Mutual labels:  yii2, yii2-extension

Yii 2 lifecycle behavior

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

Latest Stable Version Total Downloads License Build Status

Installation

This is an extension for the Yii 2 PHP framework.

Installation is recommended to be done via composer by running:

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:

"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.

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.

The following example shows how to define the allowed status changes:

	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:

Visualization of state transitions

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.

  • 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.

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:

'events' => [
    BaseActiveRecord::EVENT_BEFORE_VALIDATE => 'handleBeforeValidate',
    BaseActiveRecord::EVENT_BEFORE_UPDATE => 'handleBeforeSave',
]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].