如何配置 Symfony 工作流
How to configure a Symfony workflow
我在 Symfony 4.2 版本上工作
我做了一个Workflow Service,有3个地方draft, reviewed and published
我添加到conf/products/framework.yaml 这行代码。但我不明白代码中的 currentPlace 是什么。我用这个例子工作 https://symfony.com/doc/4.2/workflow.html
workflows:
article_publishing:
type: 'workflow'
audit_trail:
enabled: true
marking_store:
type: 'multiple_state'
arguments:
- 'currentPlace'
supports:
- App\Entity\Article
initial_marking: draft
places:
-draft
-reviewed
-published
transitions:
to_review:
from: draft
to: reviewed
publish:
from: reviewed
to: published
但是当我刷新网站时出现这个错误
Warning: array_map(): Argument #2 should be an array
Error
您的 YAML 配置的 places
部分无效。
它应该是这样的:
places:
- draft
- reviewed
- published
注意减号和项目名称之间的space
我对其进行了更改,以便从类型中删除引号并删除参数并添加 approved_by_admin,现在可以使用了。
framework:
workflows:
article_publishing:
type: workflow
audit_trail: true
marking_store:
type: multiple_state
supports:
- App\Entity\Article
places:
- draft
- for_review
- approved_by_admin
- published
transitions:
to_for_review:
from: draft
to: for_review
to_approved_by_admin:
from: for_review
to: approved_by_admin
to_published:
from: approved_by_admin
to: published
to_draft:
from: for_review
to: draft
然后我将此代码添加到 Controller
/**
* @Route("/apply-transition/{article}", methods={"POST"})
*/
public function applyTransition(WorkflowInterface $articleWorkflow, Request $request, Article $article, RouterInterface $router)
{
$slug = $article->getCategory()->getSlug();
try {
$articleWorkflow->apply($article, $request->request->get('transition'));
$this->repository->flush();
} catch (ExceptionInterface $e) {
echo $e->getMessage();
}
$url = $router->generate('app_category_view', ([
'slug' => $slug,
]));
return new RedirectResponse($url);
}
/**
* @Route("/resetMarking/{article}", methods={"POST"})
*/
public function resetMarking(Article $article, RouterInterface $router)
{
$slug = $article->getCategory()->getSlug();
$article->setMarking([]);
$this->repository->flush();
$url = $router->generate('app_category_view', ([
'slug' => $slug,
]));
return new RedirectResponse($url);
}
然后我将此添加到实体
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $marking;
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $transitionContexts;
我在 Symfony 4.2 版本上工作 我做了一个Workflow Service,有3个地方draft, reviewed and published
我添加到conf/products/framework.yaml 这行代码。但我不明白代码中的 currentPlace 是什么。我用这个例子工作 https://symfony.com/doc/4.2/workflow.html
workflows:
article_publishing:
type: 'workflow'
audit_trail:
enabled: true
marking_store:
type: 'multiple_state'
arguments:
- 'currentPlace'
supports:
- App\Entity\Article
initial_marking: draft
places:
-draft
-reviewed
-published
transitions:
to_review:
from: draft
to: reviewed
publish:
from: reviewed
to: published
但是当我刷新网站时出现这个错误
Warning: array_map(): Argument #2 should be an array
Error
您的 YAML 配置的 places
部分无效。
它应该是这样的:
places:
- draft
- reviewed
- published
注意减号和项目名称之间的space
我对其进行了更改,以便从类型中删除引号并删除参数并添加 approved_by_admin,现在可以使用了。
framework:
workflows:
article_publishing:
type: workflow
audit_trail: true
marking_store:
type: multiple_state
supports:
- App\Entity\Article
places:
- draft
- for_review
- approved_by_admin
- published
transitions:
to_for_review:
from: draft
to: for_review
to_approved_by_admin:
from: for_review
to: approved_by_admin
to_published:
from: approved_by_admin
to: published
to_draft:
from: for_review
to: draft
然后我将此代码添加到 Controller
/**
* @Route("/apply-transition/{article}", methods={"POST"})
*/
public function applyTransition(WorkflowInterface $articleWorkflow, Request $request, Article $article, RouterInterface $router)
{
$slug = $article->getCategory()->getSlug();
try {
$articleWorkflow->apply($article, $request->request->get('transition'));
$this->repository->flush();
} catch (ExceptionInterface $e) {
echo $e->getMessage();
}
$url = $router->generate('app_category_view', ([
'slug' => $slug,
]));
return new RedirectResponse($url);
}
/**
* @Route("/resetMarking/{article}", methods={"POST"})
*/
public function resetMarking(Article $article, RouterInterface $router)
{
$slug = $article->getCategory()->getSlug();
$article->setMarking([]);
$this->repository->flush();
$url = $router->generate('app_category_view', ([
'slug' => $slug,
]));
return new RedirectResponse($url);
}
然后我将此添加到实体
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $marking;
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $transitionContexts;