Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Where to start process #32

Open
foaly-nr1 opened this issue Jun 16, 2016 · 1 comment
Open

Where to start process #32

foaly-nr1 opened this issue Jun 16, 2016 · 1 comment

Comments

@foaly-nr1
Copy link

foaly-nr1 commented Jun 16, 2016

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?

@gilles-g
Copy link
Member

gilles-g commented Jun 16, 2016

Hi,

I do not recommend to use a listener or subscriber, it can become difficult to maintain, especially to handle exceptions

public function start(ModelInterface $model)
{
$modelState = $this->storage->findCurrentModelState($model, $this->process->getName());
if ($modelState instanceof ModelState) {
throw new WorkflowException(sprintf('The given model has already started the "%s" process.', $this->process->getName()));
}

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

$modelState = $this->storage->newModelStateSuccess($model, $this->process->getName(), $step->getName(), $currentModelState);

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

I hope it helped you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants