symfony / symfony-docs

The Symfony documentation
https://symfony.com/doc
Other
2.18k stars 5.12k forks source link

Not clear how initial_marking works #18578

Closed darius-v closed 1 year ago

darius-v commented 1 year ago

On creating entity, I have saved to database initial currentPlace value. I was not even making it nullable. But thankfully there is an answer https://stackoverflow.com/questions/54628243/symfony-4-workflow-initial-place-doesnt-work that it has to be null in the database when entity is created. When I was setting the value on creation, I was getting some error. I did not see in the doc how this config works and that I should let it be null on creation. https://symfony.com/doc/current/workflow.html

xabbuh commented 1 year ago

When I was setting the value on creation, I was getting some error.

What do you mean with that? What error did you get?

darius-v commented 1 year ago

When I was setting the value on creation, I was getting some error.

What do you mean with that? What error did you get?

#[Route(path: '/articles/create', name: 'create', methods: ['GET'])]
    public function create(): Response
    {
        $article = new Article();
        $article->setText(rand(1, 100));
        $article->setCurrentPlace([Article::STATE_DRAFT]);
        // we do not set current state - it has to be null initially. Initial state is set in workflow config.
        // https://stackoverflow.com/questions/54628243/symfony-4-workflow-initial-place-doesnt-work

        $this->articleRepository->save($article, true);

        return new Response(sprintf('Article created. <a href="/article/move-to-review/%d">To review</a>', $article->getId()));
    }

so there is this line

    $article->setCurrentPlace([Article::STATE_DRAFT]);

creation works with it. But then when with another method I go to review:

#[Route(path: '/article/move-to-review/{id}', methods: ['GET'])]
    public function toReview(int $id): Response
    {
        $article = $this->articleRepository->findOneBy(['id' => $id]);

        // Update the currentState on the post
        $this->blogPublishingWorkflow->apply($article, 'to_review');

        return new Response('Article now is in review');
    }

I get

Symfony\Component\Workflow\Exception\LogicException:
Place "0" is not valid for workflow "blog_publishing".

  at vendor/symfony/workflow/Workflow.php:111
  at Symfony\Component\Workflow\Workflow->getMarking(object(Article), array())
     (vendor/symfony/workflow/Workflow.php:173)
  at Symfony\Component\Workflow\Workflow->apply(object(Article), 'to_review')
     (src/Controller/ArticlesController.php:49)
  at App\Controller\ArticlesController->toReview(12)
     (vendor/symfony/http-kernel/HttpKernel.php:163)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:184)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor/autoload_runtime.php:29)
  at require_once('/var/www/symfony/vendor/autoload_runtime.php')
     (public/index.php:5)                

If I remove that line, for setting draft on creation, things work well.

OskarStark commented 1 year ago

Are you sure you should set this value as array?

darius-v commented 1 year ago

I am now sure that I do not need to set this value at all, it has to be set in workflow config. But I could not see in documenation that I have to not set this value. I thought I have to, otherwise how it will know. But it was hard to notice that it works differently. As I said I only found it in stack overflow answer. It would be best if we could find that easily in symfony documentation.