{"id":14966761,"url":"https://github.com/cebe/yii2-lifecycle-behavior","last_synced_at":"2025-10-25T17:30:37.078Z","repository":{"id":28336228,"uuid":"31849423","full_name":"cebe/yii2-lifecycle-behavior","owner":"cebe","description":"Define the lifecycle of a model by defining allowed status changes.","archived":false,"fork":false,"pushed_at":"2018-12-21T12:58:29.000Z","size":34,"stargazers_count":50,"open_issues_count":1,"forks_count":3,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-01-31T09:33:59.971Z","etag":null,"topics":["state-machine","state-transitions","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cebe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-08T12:49:02.000Z","updated_at":"2024-07-14T17:44:29.000Z","dependencies_parsed_at":"2022-07-27T13:49:25.476Z","dependency_job_id":null,"html_url":"https://github.com/cebe/yii2-lifecycle-behavior","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cebe%2Fyii2-lifecycle-behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cebe%2Fyii2-lifecycle-behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cebe%2Fyii2-lifecycle-behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cebe%2Fyii2-lifecycle-behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cebe","download_url":"https://codeload.github.com/cebe/yii2-lifecycle-behavior/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238183696,"owners_count":19430172,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["state-machine","state-transitions","yii2","yii2-extension"],"created_at":"2024-09-24T13:36:54.334Z","updated_at":"2025-10-25T17:30:36.749Z","avatar_url":"https://github.com/cebe.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Yii 2 lifecycle behavior\n========================\n\nDefine the lifecycle of a model by defining allowed status changes in terms of a state machine.\n\n[![Latest Stable Version](https://poser.pugx.org/cebe/yii2-lifecycle-behavior/v/stable)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)\n[![Total Downloads](https://poser.pugx.org/cebe/yii2-lifecycle-behavior/downloads)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)\n[![License](https://poser.pugx.org/cebe/yii2-lifecycle-behavior/license)](https://packagist.org/packages/cebe/yii2-lifecycle-behavior)\n[![Build Status](https://travis-ci.org/cebe/yii2-lifecycle-behavior.svg?branch=master)](https://travis-ci.org/cebe/yii2-lifecycle-behavior)\n\n\nInstallation\n------------\n\nThis is an extension for the [Yii 2](http://www.yiiframework.com/) PHP framework.\n\nInstallation is recommended to be done via [composer][] by running:\n\n\tcomposer require cebe/yii2-lifecycle-behavior\n\nAlternatively you can add the following to the `require` section in your `composer.json` manually\nand run `composer update` afterwards:\n\n```json\n\"cebe/yii2-lifecycle-behavior\": \"~2.0.0\"\n```\n\n[composer]: https://getcomposer.org/ \"The PHP package manager\"\n\n\nUsage\n-----\n\nYou can add the behavior to an [ActiveRecord][] class. It does not work with `yii\\base\\Model`\nbecause it relies on the old-attribute feature which is only available in active record.\n\nYou can add the behavior to the model by creating a `behaviors()` method if there is none yet, or\nadd it to the list of exising behaviors.\n\nThe following example shows how to define the allowed status changes:\n\n```php\n\tpublic function behaviors()\n\t{\n\t\treturn [\n\t\t\t'lifecycle' =\u003e [\n\t\t\t\t'class' =\u003e cebe\\lifecycle\\LifecycleBehavior::class,\n\t\t\t\t'validStatusChanges' =\u003e [\n\t\t\t\t\t'draft'     =\u003e ['ready', 'delivered'],\n\t\t\t\t\t'ready'     =\u003e ['draft', 'delivered'],\n\t\t\t\t\t'delivered' =\u003e ['payed', 'archived'],\n\t\t\t\t\t'payed'     =\u003e ['archived'],\n\t\t\t\t\t'archived'  =\u003e [],\n\t\t\t\t],\n\t\t\t],\n\t\t];\n\t}\n```\n\nThe above state transitions can be visualized as the following state machine:\n\n![Visualization of state transitions](example.png)\n\n[ActiveRecord]: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html\n\n## Status field validation\n\nBy default, the behavior will validate the `status` attribute of the record, when `validate()` or `save()` is called\nand add a validation error in case state has changed in a way that is not allowed.\n\n- The attribute to validate can be configured by setting the `statusAttribute` property of the behavior.\n- The error message can be configured by setting the `validationErrorMessage` property of the behavior.\n  The place holders `{old}` and `{new}` are being replaced with the corresponding status values.\n\n## Program flow validation\n\nThe behavior may also be used to validate status changes in program flow. This is different to user input validation as\ndescribed above, because program flow will be aborted by an [exception](src/StatusChangeNotAllowedException.php) in this case.\nFor user input, the recipient of the error message is the user, when status is not changed by the user,\nthe recipient of the error is the developer.\n\n## Configuring different validation methods\n\nBy default status field is validated both, on validation and on update. To disable one of the methods, you may configure\nthe `$events` propery, which is by default:\n\n```php\n'events' =\u003e [\n    BaseActiveRecord::EVENT_BEFORE_VALIDATE =\u003e 'handleBeforeValidate',\n    BaseActiveRecord::EVENT_BEFORE_UPDATE =\u003e 'handleBeforeSave',\n]\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcebe%2Fyii2-lifecycle-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcebe%2Fyii2-lifecycle-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcebe%2Fyii2-lifecycle-behavior/lists"}