我现在不知道是否很多人已经测试了symfony workflow组件,但我希望你们中的一些人有:)

因此,我在两个对象上使用此组件,并且我希望第一个对象更新,然后将第二个更新到它取决于所应用的transition

为此,我在自己的第一个对象上使用了workflow guard listener,并尝试在我的第二个对象上使用workflow::apply(使用第二个工作流程...)

问题是,当我制作workflow::can时,该事件是分派(dispatch)的,它试​​图在我的第二个对象应用一个新的状态...这是很不正常的,因为我只是问我是否可以将应用于某些过渡,而不是,向询问,实际上将应用于我的第一个对象

配置

framework:
    workflows:
        request_for_operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\RequestForOperation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                request_for_operations:
                    from: draft
                    to:   pending_for_management
                start_rfop_management:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled
        operation:
            type: 'state_machine'
            marking_store:
                type: 'single_state'
                arguments:
                    - 'status'
            supports:
                - AppBundle\Entity\Operation
            places:
                - draft
                - pending_for_management
                - in_progress
                - finished
                - canceled
            transitions:
                validate_operation:
                    from: draft
                    to:   pending_for_management
                start_tracking:
                    from: pending_for_management
                    to:   in_progress
                close:
                    from: in_progress
                    to:   finished
                cancel:
                    from: [pending_for_management, in_progress]
                    to:   canceled

订户
class RequestForOperationListener implements EventSubscriberInterface
{
    public function __construct(
        OperationManager $operationManager,
        UserNotifier $userNotifier
    ) {
        $this->operationManager = $operationManager;
        $this->userNotifier = $userNotifier;
    }

    public static function getSubscribedEvents()
    {
        return [
            'workflow.request_for_operation.guard.request_for_operations' => ['onRequestForOperations'],
            'workflow.request_for_operation.guard.start_rfop_management' => ['onStartRfopManagement'],
            'workflow.request_for_operation.guard.close' => ['onClose'],
            'workflow.request_for_operation.guard.cancel' => ['onCancel'],
        ];
    }

    public function onRequestForOperations(GuardEvent $event)
    {
        /** @var RequestForOperation $rfop */
        $rfop = $event->getSubject();

        //get all the operations linked to the rfop
        $operations = $rfop->getOperations();
        foreach ($operations as $operation) {
            //set the status of the operation to 'pending_for_management'
            $this->operationManager->applyTransition($operation, 'validate_operation');
            //set the status of the sub-operations to 'pending_for_management'
            foreach ($operation->getChildren() as $subOperation) {
                $this->operationManager->applyTransition($subOperation, 'validate_operation');
            }

            //get the users (i.e: managers) linked to the operation and notify them (by mail or whatever)
            $this->notifyAssignedUsers($operation->getUsers(), $operation);
        }
    }
}

最佳答案

我想我遇到了与您相同的问题,并且我已经在Symfony项目中记录了一个错误:https://github.com/symfony/symfony/issues/33105

关于php - Symfony工作流::可以通过工作流 guard 事件监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41466939/

10-13 09:22
查看更多