use raoul2000\workflow\base\SimpleWorkflowBehavior; public function behaviors() { return [ 'simpleWorkflow' => [ 'class' => SimpleWorkflowBehavior::className() ], ]; }To learn more about Yii2 behaviors refer to the Yii2 Definitive Guide You can customize the *SimpleWorkflowBehavior* with the following parameters : - statusAttribute : name of the attribute that is used by the owner model to hold the status value. The default value is "status". - defaultWorkflowId : identifier of the default workflow for the owner model. If no value is provided, the behavior creates a default workflow identifier (see [[getDefaultWorkflowId]]). - source : name of the *Workflow Source Component* that the behavior uses to read the workflow definition. By default the component id "workflowSource" is used. If it is not already available in the current application it is created by the behavior using the default workflow source component class. Below is an example behavior initialization :
use raoul2000\workflow\base\SimpleWorkflowBehavior; public function behaviors() { return [ 'simpleWorkflow' => [ 'class' => SimpleWorkflowBehavior::className(), 'statusAttribute' => 'col_status', 'defaultWorkflowId' => 'MyWorkflow', 'source' => 'myWorkflowSource', ], ]; }Exibir arquivo Open project: raoul2000/yii2-workflow Class Usage Examples
Property | Type | Description | |
---|---|---|---|
$autoInsert | (**not enabled**) if TRUE, the model is automatically inserted into the default workflow. If $autoInsert contains a string, it is assumed to be an initial status Id that will be used to set the status. If FALSE (default) the status is not modified. | ||
$eventSequence | name of the event sequence provider component. If the component does not exist it is created by this behavior using the default event sequence class. Set this attribute to NULL if you are not going to use any Workflow Event. | ||
$fireDefaultEvent | When TRUE, a default event is fired on each status change, including when the model enters or leaves the workflow and even if no event sequence is configured. When FALSE the default event is not fired. | ||
$propagateErrorsToModel | If TRUE, all errors that may be registred on an invalidated 'before' event, are assigned to the status attribute of the owner model. | ||
$source | name of the workflow source component to use with the behavior | ||
$statusAccessor | The status accessor component definition or NULL (default) if no
status accessor is used by this behavior. When not null, the value of this attribute can be specified in one of the following forms : - string : name of an existing status accessor component registered in the current Yii::$app. - object : the instance of the status converter Note that the status accessor configured here must implement the raoul2000\workflow\base\IStatusAccessor interface. |
||
$statusAttribute | name of the owner model attribute used to store the current status value. It is also possible to use a model property but in this case you must provide a suitable status accessor component that will handle status persistence. | ||
$statusConverter | The status converter component definition or NULL (default) if no
status converter is used by this behavior. When not null, the value of this attribute can be specified in one of the following forms : - string : name of an existing status converter component registered in the current Yii::$app. - object : the instance of the status converter Note that the status converter configured here must implement the raoul2000\workflow\base\IStatusIdConverter interface. |
||
$stopOnFirstInvalidEvent | if TRUE, all "before" events are fired even if one of them is invalidated by an attached handler. When FALSE, the first invalidated event interrupts the event sequence. Note that if an event is attached to several handlers, they will all be invoked unless the event is invalidated and marked as handled. |
Method | Description | |
---|---|---|
__construct ( array $config = [] ) | Instance constructor. | |
afterDelete ( |
Fires pending events, once the owner model has been successfully deleted. | |
afterSaveStatus ( |
After the owner model has been saved, fire pending events. | |
attach ( $owner ) | Attaches the behavior to the model. | |
beforeDelete ( |
Handle the case where the owner model is leaving the workflow. | |
beforeSaveStatus ( |
Send owner model into status if needed. | |
createTransitionItems ( mixed $status, boolean $WithScenarioNames, boolean $withEventSequence ) : array | Creates and returns workflow event sequence and/or scenario for the pending transition. | |
enterWorkflow ( string $workflowId = null ) : boolean | Puts the owner model into the workflow $workflowId or into its default workflow if no argument is provided. | |
events ( ) | Install events handlers. | |
getDefaultWorkflowId ( ) : string | Returns the id of the default workflow associated with the owner model. | |
getEventSequence ( string $status ) : |
Creates and returns the list of events that will be fire when the owner model is sent from its current status to the one passed as argument. | |
getNextStatuses ( $validate = false, $beforeEvents = false ) : array | Returns all status that can be reached from the current status. | |
getScenarioSequence ( string $status ) : string[] | Creates and returns the list of scenario names that will be used to validate the owner model when it is sent from its current status to the one passed as argument. | |
getStatusAccessor ( ) : null | raoul2000\workflow\base\IStatusAccessor | Returns the status accessor instance used by this behavior or NULL if no status accessor is used. | |
getStatusConverter ( ) : null | raoul2000\workflow\base\IStatusIdConverter | Returns the status converter instance used by this behavior or NULL if no status converter is used. | |
getWorkflow ( ) : null | |
Returns the current Workflow instance the model is in or NULL if the model is not in a workflow. | |
getWorkflowSource ( ) : raoul2000\workflow\source\IWorkflowSource | Returns the *Workflow Source Component* used by this behavior. | |
getWorkflowStatus ( ) : null | |
Returns the current Status instance. | |
hasWorkflowStatus ( ) : boolean | Returns a value indicating whether the owner model is currently in a workflow or not. | |
init ( ) | Initialize the behavior. | |
initStatus ( ) | Initialize the internal status value based on the owner model status attribute. | |
isAttachedTo ( |
Tests that a SimpleWorkflowBehavior behavior is attached to the object passed as argument. | |
sendToStatus ( |
Send the owner model into the status passed as argument. | |
statusEquals ( |
Tests if the current status is equal to the status passed as argument. |
Method | Description | |
---|---|---|
doAutoInsert ( ) | NOT IMPLEMENTED | |
ensureStatusInstance ( mixed $mixed, boolean $strict = false ) : |
Returns a Status instance for the value passed as argument. | |
firePendingEvents ( ) | Send pending events. | |
getOwnerStatus ( ) : string | Returns the value stored in the [[statusAttribute]] attribute of the owner model. | |
selectDefaultWorkflowId ( ) : string | Returns the default workflow ID to use with this model. | |
sendToStatusInternal ( mixed $status, boolean $delayed ) : boolean | Performs status change and event fire. | |
setStatusInternal ( |
Set the internal status value and the owner model status attribute. |
public __construct ( array $config = [] ) | ||
$config | array |
public afterDelete ( |
||
$event |
public afterSaveStatus ( |
||
$event |
public attach ( $owner ) |
public beforeDelete ( |
||
$event |
public beforeSaveStatus ( |
||
$event |
public createTransitionItems ( mixed $status, boolean $WithScenarioNames, boolean $withEventSequence ) : array | ||
$status | mixed | a status Id or a Status instance considered as the target status to reach |
$WithScenarioNames | boolean | When TRUE scenario names are requested, FALSE otherwise |
$withEventSequence | boolean | When TRUE the event sequence is requested, FALSE otherwise |
return | array | Three elements : Status intance, scenario names, event sequence. |
public enterWorkflow ( string $workflowId = null ) : boolean | ||
$workflowId | string | the ID of the workflow the owner model must be inserted to or NULL to insert into the model's default workflow. |
return | boolean | TRUE if the operation succeeded, FALSE otherwise |
public events ( ) |
public getDefaultWorkflowId ( ) : string | ||
return | string | id for the workflow the owner model is in. |
public getEventSequence ( string $status ) : |
||
$status | string | the target status |
return | The list of events |
[ targetStatusId => [ 'status' => the status instance the 'validation' key is present only if $validate is true 'validation' => [ 0 => [ 'scenario' => scenario name 'success' => true (validation success) | false (validation failure) | null (no validation for this scenario) ], 1 => [ ... ] ], the 'event' key is present only if $beforeEvent is TRUE 'event' => [ 0 => [ 'name' => event name 'success' => true (event handler success) | false (event handler failed : the event has been invalidated) | null (no event handler) ] 1 => [...] ], if $validate is true or if $beforeEvent is TRUE 'isValid' => true (being given the verifications that were done, the target status can be reached) | false (being given the verifications that were done, the target status cannot be reached) ], anotherStatusID => [ ...] ]If the owner model is not currently in a workflow, this method returns the initial status of its default workflow for the model.
public getNextStatuses ( $validate = false, $beforeEvents = false ) : array | ||
return | array | list of status |
public getScenarioSequence ( string $status ) : string[] | ||
$status | string | the target status |
return | string[] | list of scenario names |
public getStatusAccessor ( ) : null | raoul2000\workflow\base\IStatusAccessor | ||
return | null | raoul2000\workflow\base\IStatusAccessor | the status accessor component used bu the behavior or NULL if not configured (default) |
public getStatusConverter ( ) : null | raoul2000\workflow\base\IStatusIdConverter | ||
return | null | raoul2000\workflow\base\IStatusIdConverter | The status Id Converter used by the behavior or NULL if not configured (default) |
public getWorkflow ( ) : null | |
||
return | null | |
the workflow the owner model is currently in, or null if the owner model is not in a workflow |
public getWorkflowSource ( ) : raoul2000\workflow\source\IWorkflowSource | ||
return | raoul2000\workflow\source\IWorkflowSource | the workflow source component instance used by this behavior |
public getWorkflowStatus ( ) : null | |
||
return | null | |
The status instance or NULL if the model is not in a workflow |
public hasWorkflowStatus ( ) : boolean | ||
return | boolean | TRUE if the owner model is in a workflow, FALSE otherwise |
public init ( ) |
public initStatus ( ) |
public static isAttachedTo ( |
||
$model | the model to test. | |
return | boolean | TRUE if at least one SimpleWorkflowBehavior behavior is attached to $model, FALSE otherwise |
public sendToStatus ( |
||
$status | the destination status to reach. If NULL, then the owner model is going to leave its current workflow. | |
return | boolean | TRUE if the transition could be performed, FALSE otherwise |
$post->statusEquals('draft'); $post->statusEquals($otherPost->getWorkflowStatus());
public statusEquals ( |
||
$status | the status to test | |
return | boolean |
public $autoInsert |
public $eventSequence |
public $fireDefaultEvent |
public $propagateErrorsToModel |
public $source |
public $statusAccessor |
public $statusAttribute |
public $statusConverter |
public $stopOnFirstInvalidEvent |