我在新的symfony(3.2)工作流功能方面遇到了一些麻烦。我似乎只能将“审核”过渡应用于任务。
$workflow->apply($task, 'review');
看这段代码有用。
拒绝和审查我总是遇到这个错误。
这里是控制器和实体:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
class WorkflowController extends Controller
{
/** @Route("/workflow", name="workflow") */
public function indexAction(
Request $request
) {
$registry = new Registry();
$definition = (new DefinitionBuilder())
->addPlaces(['drafted', 'reviewed', 'rejected', 'published'])
->addTransition(new Transition('review', 'drafted', 'reviewed'))
->addTransition(new Transition('publish', 'reviewed', 'published'))
->addTransition(new Transition('reject', 'reviewed', 'rejected'))
->build();
$workflow = new Workflow(
$definition,
new SingleStateMarkingStore('state')
);
$task = new \AppBundle\Entity\Task();
$workflow->apply($task, 'reject');
$registry->add($workflow, \AppBundle\Entity\Task::class);
return new Response(json_encode([
'available_actions' => [
'review' => $workflow->can($task, 'review'),
'publish' => $workflow->can($task, 'publish'),
'reject' => $workflow->can($task, 'reject'),
]]
));
}
}
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Task
{
private $id;
private $title;
public $state;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
}
有人可以帮我解决问题吗?
答案 0 :(得分:1)
是的,您只能对新创建的任务应用review
,因为它们处于第一个状态drafted
。要发布或拒绝任务,您首先需要应用review
,然后reject
或publish
:
$task = new Task();
// Place newly created task to reviewed status
$workflow->apply($task, 'reject');
// Then reject or publish
$workflow->apply($task, 'publish')
我会尽力解释一下。首先,您可以定义任务的状态:
->addPlaces(['drafted', 'reviewed', 'rejected', 'published'])
新任务获得drafted
(列表中的第一个)状态,因为您省略了Task
s state
属性的设置值。
接下来,您定义任务从一个状态传递到另一个状态的规则(称为Transition
)
->addTransition(new Transition('review', 'drafted', 'reviewed'))
它刚刚定义了名为review
的转换(或操作),仅适用于drafted
任务,在执行任务后,其状态更改为reviewed
。
因此,总而言之,首先应审查所有新创建的任务,然后发布或拒绝。