PHPackages                             cebe/yii2-lifecycle-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. cebe/yii2-lifecycle-behavior

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

cebe/yii2-lifecycle-behavior
============================

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

2.0.0(7y ago)5119.8k↓50%3[1 issues](https://github.com/cebe/yii2-lifecycle-behavior/issues)1MITPHPPHP &gt;=5.6.0

Since Mar 8Pushed 7y ago9 watchersCompare

[ Source](https://github.com/cebe/yii2-lifecycle-behavior)[ Packagist](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)[ Docs](https://github.com/cebe/markdown#readme)[ RSS](/packages/cebe-yii2-lifecycle-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (1)

Yii 2 lifecycle behavior
========================

[](#yii-2-lifecycle-behavior)

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

[![Latest Stable Version](https://camo.githubusercontent.com/2c32a475eff02cf821153f81e6d3dacc996197c555d8cf4cb3be0e271fbc26e3/68747470733a2f2f706f7365722e707567782e6f72672f636562652f796969322d6c6966656379636c652d6265686176696f722f762f737461626c65)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)[![Total Downloads](https://camo.githubusercontent.com/a8e5a1961f8d396bde217464f8abe4b3f786ab175e7a4622ed34863bda51a6c6/68747470733a2f2f706f7365722e707567782e6f72672f636562652f796969322d6c6966656379636c652d6265686176696f722f646f776e6c6f616473)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)[![License](https://camo.githubusercontent.com/9e600ec025a96fc3ab17e24442da3dcd4ed1482ff865abcc43864f164a2b1131/68747470733a2f2f706f7365722e707567782e6f72672f636562652f796969322d6c6966656379636c652d6265686176696f722f6c6963656e7365)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)[![Build Status](https://camo.githubusercontent.com/8c01921d0d91e1291f0e65a41b4498216a9ca9d2c123f986ba19ce699326b9fa/68747470733a2f2f7472617669732d63692e6f72672f636562652f796969322d6c6966656379636c652d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cebe/yii2-lifecycle-behavior)

Installation
------------

[](#installation)

This is an extension for the [Yii 2](http://www.yiiframework.com/) PHP framework.

Installation is recommended to be done via [composer](https://getcomposer.org/ "The PHP package manager") 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
-----

[](#usage)

You can add the behavior to an [ActiveRecord](http://www.yiiframework.com/doc-2.0/guide-db-active-record.html) 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](example.png)](example.png)

Status field validation
-----------------------

[](#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
-----------------------

[](#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](src/StatusChangeNotAllowedException.php) 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
----------------------------------------

[](#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',
]
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1383 days

Total

2

Last Release

2697d ago

Major Versions

1.0.0 → 2.0.02018-12-21

PHP version history (2 changes)1.0.0PHP &gt;=5.4.0

2.0.0PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/261a6249c6f605f3956a2fae40fbb813f6b2e1e6f2bf806180c851a965426e54?d=identicon)[cebe](/maintainers/cebe)

---

Top Contributors

[![cebe](https://avatars.githubusercontent.com/u/189796?v=4)](https://github.com/cebe "cebe (13 commits)")

---

Tags

state-machinestate-transitionsyii2yii2-extensionvalidationmodelyii2lifecycle

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cebe-yii2-lifecycle-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/cebe-yii2-lifecycle-behavior/health.svg)](https://phpackages.com/packages/cebe-yii2-lifecycle-behavior)
```

###  Alternatives

[borales/yii2-phone-input

Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

1341.6M6](/packages/borales-yii2-phone-input)[codeonyii/yii2-at-least-validator

Validates at least one (or more) attributes.

28253.5k1](/packages/codeonyii-yii2-at-least-validator)[usmanhalalit/dryval

Simplify validation in Laravel.

394.7k](/packages/usmanhalalit-dryval)[ikkez/f3-validation-engine

Validation engine for Cortex and the PHP Fat-Free Framework

1221.0k1](/packages/ikkez-f3-validation-engine)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
