Skip to main content

yii2-workflow扩展

composer

"raoul2000/yii2-workflow":"*",

app\models 文件下WorkOrderWorkflow.php 设置流程

namespace app\models;

class WorkOrderWorkflow implements \raoul2000\workflow\source\file\IWorkflowDefinitionProvider
{
public function getDefinition() {
return [
'initialStatusId' => '创建',
'status' => [
'创建' => [
'transition' => ['审批中']
],
'审批中' => [
'transition' => ['审批完成','创建']
],
'审批完成' => [
'transition' => []
]
]
];
}
}

initialStatusId 初始状态

status 工作流的步骤

transition

注意:1. 这些值默认都应该是英文,是WorkflowFileSource文件中的PATTERN_ID变量设置了正则匹配我把正则的规则给修改了const PATTERN_ID = '/.*/';

  2. 工作流步骤中的所有transition数组中的值,status 的key都要包含。不然报错。

models文件配置

namespace app\models;

class WorkOrder extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'simpleWorkflow' => [
'class' => \raoul2000\workflow\base\SimpleWorkflowBehavior::className(),
'statusAttribute' => 'status',
'defaultWorkflowId' => 'WorkOrderWorkflow',
'source' => 'WorkOrder',
],
];
}
}

class指向的是扩展文件

statusAttribute:数据库中那个字段记录工作流的状态

defaultWorkflowId:设置工作流的规则的文件

source:数据库表对应models文件

使用

//创建时候用
$workOrder = new WorkOrder();
$workOrder->status = '创建';
$workOrder->save();
echo 'post status is : '. $workOrder->workflowStatus->label;

//修改的时候
$WorkOrder = new WorkOrder();
$work = $WorkOrder::find()->where('id=22')->one();
$work->status='审批中';
$work->save();
echo 'post status is : '. $work->workflowStatus->label;

//判断的时候
$WorkOrder = new WorkOrder();
$work = $WorkOrder::find()->where('id=23')->one();
$work->sendToStatus('审批完成');
echo 'post status is : '. $work->workflowStatus->label;