John Davidson

php - Workflow in Symfony doesn't work with "multiple_state"

0 comments
Message:


In one of the configuration files of Symfony 5.2.8 with Workflow 5.2.7 I have:


    framework:
workflows:
register_participation:
type: 'workflow' # or 'state_machine'
audit_trail:
enabled: true
marking_store:
type: 'multiple_state'
arguments:
- complexState
# [...]

When I execute bin/console I have error:



Unrecognized option "arguments" under framework.workflows.workflows.register_participation.marking_store". Available options are "property", "service", "type".



When I change the configuration to:


    framework:
workflows:
register_participation:
type: 'workflow' # or 'state_machine'
audit_trail:
enabled: true
marking_store:
type: 'multiple_state'
property: state
# [...]

I get the error:



The value "multiple_state" is not allowed for path framework.workflows.workflows.register_participation.marking_store.type". Permissible values: "method"



It works when I change to this:


        marking_store:
type: 'method'
property: main_state

Anybody have idea what can I do to works with multiple_state? Thanks in advance.



My Answer:
In Symfony, the workflow component allows you to define a set of states and transitions for an object, and then manage the object's state changes based on those transitions. Here is a basic workflow example in Symfony:

1. Define the workflow configuration in a YAML file:

yaml
framework:
workflows:
article:
type: 'workflow'
marking_store:
type: 'multiple_state'
supports:
- App\Entity\Article
places:
- draft
- review
- published
transitions:
submit:
from: 'draft'
to: 'review'
publish:
from: 'review'
to: 'published'


2. Create a workflow service in your Symfony application:

php
namespace App\Service;

use Symfony\Component\Workflow\WorkflowInterface;

class ArticleWorkflowService
{
private $workflow;

public function __construct(WorkflowInterface $workflow)
{
$this->workflow = $workflow;
}

public function submitArticle(Article $article)
{
if ($this->workflow->can($article, 'submit')) {
$this->workflow->apply($article, 'submit');
}
}

public function publishArticle(Article $article)
{
if ($this->workflow->can($article, 'publish')) {
$this->workflow->apply($article, 'publish');
}
}
}


3. Use the workflow service in your controller or other services:

php
namespace App\Controller;

use App\Entity\Article;
use App\Service\ArticleWorkflowService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class ArticleController extends AbstractController
{
public function submit(Article $article, ArticleWorkflowService $workflowService): Response
{
$workflowService->submitArticle($article);

return $this->redirectToRoute('article_show', ['id' => $article->getId()]);
}

public function publish(Article $article, ArticleWorkflowService $workflowService): Response
{
$workflowService->publishArticle($article);

return $this->redirectToRoute('article_show', ['id' => $article->getId()]);
}
}


This is a basic example of how you can implement a workflow in Symfony using the workflow component. You can define more complex workflows with multiple states and transitions, as well as add guards and events to control the workflow behavior.

Rate this post

5 of 5 based on 9316 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us