LexikWorkflowBundle icon indicating copy to clipboard operation
LexikWorkflowBundle copied to clipboard

Where to start process

Open jonny-no1 opened this issue 9 years ago • 1 comments

This is more of a support query. Once I've fully understood this bundle, I would love to contribute with a pull request to the README file.

The line $processHandler->start($model); from your Usage example needs to be called once an entity is persisted for the first time. I hence thought I'd use an entity listener or lifecycle subscriber to handle it, but am getting a circular reference. Alternatively, I could implement checks in my form handler to call the method when the entity is first persisted.

What do you guys do in your projects – where do you call it?

jonny-no1 avatar Jun 16 '16 12:06 jonny-no1

Hi,

I do not recommend to use a listener or subscriber, it can become difficult to maintain, especially to handle exceptions https://github.com/lexik/LexikWorkflowBundle/blob/ceb41e4523c8c2ae2be2658d98e0f14d6fb83bae/Handler/ProcessHandler.php#L69-L76

In my experience, I use it with listener and I don't like it. Then, I used it in the controller it's more simple but too easy. Or, if you use a form handler or Command Bus, you can start the process from there, and begin transaction > try/catch > flush in your controller.

why begin transaction? When you start process, we saved a modelState https://github.com/lexik/LexikWorkflowBundle/blob/ceb41e4523c8c2ae2be2658d98e0f14d6fb83bae/Handler/ProcessHandler.php#L155

If you have an exception, error, you will have an modelState flushed but you don't need it.

I hope it helped you

gilles-g avatar Jun 16 '16 13:06 gilles-g