netis / yii2-fsm
Extensions to the Yii PHP framework providing tools for switching status of an AR model.
Installs: 147
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Type:yii2-extension
Requires
- netis/yii2-crud: *
- yiisoft/yii2: >=2.0.4
This package is not auto-updated.
Last update: 2024-10-26 19:27:11 UTC
README
Extensions to the Yii PHP framework providing tools for switching status of a model.
Provides:
- a controller action with a view to perform status transitions
- a model behavior to bind custom logic to status transitions
- a controller method to build a menu
- state graph configuration
Installation
Currently, the repository is private, so add it manually to the composer.json file:
"require": { "netis/yii2-fsm": "dev-master" },
add alias to config
'aliases' => [ '@netis' => '@vendor/netis', ],
Usage
run example `./yii fsm/create "\netis\assortment\models\Product" "application_status_changes" "productPricings"`
- Implement the
IStateful
interface in selected AR model. You might want to addYii::import('fsm.components.*')
at the top of the file. - Adjust rules to remove attributes from the
transition
scenario. TheNetActiveRecord.filterRules()
helper method should be used for that. - In the controller, add the
state
action and include it in the context menu, adjust according to comments:
public function actions() { return array( 'state'=>array( 'class'=>'fsm.components.StateAction', 'updateAuthItem' => 'update Model', // adjust here, insert AR model name 'isAdminCallback' => array('NetController', 'isAdmin'), ), ); } protected function buildNavigation(CAction $action, NetActiveRecord $model, $readOnly = false, $horizontal = true) { $result = parent::buildNavigation($action, $model, $readOnly, $horizontal); if ($horizontal || $model->primaryKey !== null) { Yii::import('fsm.components.StateAction'); $transitions = $model->getTransitionsGroupedByTarget(); $this->menu[] = StateAction::getContextMenuItem($action, $transitions, $model, $model->status_id, self::isAdmin()); // adjust status column } return $result; }
Todo
- group state transition logic into named workflows