问题描述:

I don't now if a lot of you have already tested the symfony workflow component but I hope some of you have :)

So, I am using this component on two objects and I want the first object to update the second one depending on the transition it is applying.

To do so, I am using the workflow guard listener on my first object and try to do a workflow::apply on my second object (using the second workflow ...).

The problem is that, when I make a workflow::can, the event is dispatched and it tries to apply a new status on my second object ... This is quite not normal as I am just asking if I can apply some transition and not asking to actually apply it on my first object.

config

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

Subscriber

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);

}

}

}

相关阅读:
Top