Symfony3:多步形成并在转发后刷新

Symfony3 : multi-steps form and flush after forward

我有一个表格可以在我的数据库中添加一个新的处方者。第一步包括告知开处方者的各种信息。

然后,我在添加之前检查是否有类似的处方者(第 2 步,第 2 表),如果有,我会要求用户确认。

简而言之,我有一个 1 步表格或 2 步表格,具体取决于重复项。

我尝试使用 CraueFormFlowBundle,但我不知道如何实施我的条件第二步。我的测试没有定论。所以我决定在我的控制器中使用 forward 方法,我喜欢它!

但是,我无法在第二步结束时(转发后)刷新我的处方,我遇到了这个错误:Unable to guess how to get a Doctrine instance from the request information for parameter "prescriber".

addAction(=步骤 1)

/**
 * Add a new prescriber
 *
 * @Route("/prescribers/add", name="prescriber_add")
 */
public function addAction(Request $request) {
    $em = $this->getDoctrine()->getManager();
    $rp = $em->getRepository('AppBundle:Prescriber');
    $p  = new Prescriber();

    // build the form
    $form = $this->createForm(AddPrescriberType::class, $p);

    // handle the submit
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        # search if a prescriber already exists
        $qb = $rp->createQueryBuilder('p');
        $qb->where($qb->expr()->eq('p.rpps', ':rpps'))
            ->orWhere($qb->expr()->andX(
                $qb->expr()->like('p.lastname', ':name'),
                $qb->expr()->like('p.firstname', ':firstname')
            ))
            ->setParameter('rpps', $p->getRpps())
            ->setParameter('name', '%'.$p->getLastname().'%')
            ->setParameter('firstname', '%'.$p->getFirstname().'%');

        $duplicates = $qb->getQuery()->getArrayResult();

        # there are duplicates
        if (!empty($duplicates)) {
            $em->persist($p);

            // confirm the addition of the new prescriber
            $params = array('prescriber' => $p, 'duplicates' => $duplicates);
            $query = $request->query->all();
            return $this->forward('AppBundle:Prescriber:addConfirm', $params, $query);

        } else {
            $em->persist($p);       # save the prescriber
            $em->flush();           # update database

            $this->addFlash('p_success', 'The prescriber has been created successfully');
            return $this->redirectToRoute('prescriber');
        }
    }

    // show form
    return $this->render('prescribers/form-step1.html.twig', array(
        'form' => $form->createView()
    ));
}

addConfirmAction(=步骤 2)

/**
 * Confirm addition of a new prescriber
 *
 * @Route("/prescribers/add/confirm", name="prescriber_add_confirm")
 */
public function addConfirmAction(Prescriber $prescriber, $duplicates, Request $request) {
    $em = $this->getDoctrine()->getManager();

    $form = $this->createFormBuilder()->getForm();

    if ($form->handleRequest($request)->isValid()) {
      $em->persist($prescriber);
      $em->flush();

      $this->addFlash('p_success', 'Prescriber has been created successfully');
      return $this->redirectToRoute('prescriber');
    }

    // show confirm page
    return $this->render('prescribers/form-step2.html.twig', array(
      'h1_title'  => 'Ajouter un nouveau prescripteur',
      'form'      => $form->createView(),
      'p'         => $prescriber,
      'duplicates'=> $duplicates
  ));
}

我认为问题出在我提交了 2 个表单...

我使用 会话 找到了解决方案。 (我知道这不是一个完美的方法,但我没有找到其他方法)

对于 Symfony 3.3.*

use Symfony\Component\HttpFoundation\Session\SessionInterface;

public function addAction(Request $request, SessionInterface $session) {
    // [...]

    # there are duplicates
    if (!empty($duplicates)) {
        $data = $form->getData();
        $session->set('prescriber', $data);
        $session->set('duplicates', $duplicates);

        return $this->forward('AppBundle:Prescriber:addConfirm');

    // [...]
}

public function addConfirmAction(Request $request, SessionInterface $session) {
    $em   = $this->getDoctrine()->getManager();
    $p    = $session->get('prescriber');
    $duplicates = $session->get('duplicates');

    // empty form with only a CSRF field
    $form = $this->createFormBuilder()->getForm();

    if ($form->handleRequest($request)->isValid()) {
        $em->persist($p);
        $em->flush();

        $this->addFlash('p_success', 'The prescriber has been created successfully');
        return $this->redirectToRoute('prescriber');
    }

    // show confirm page
    return $this->render('prescribers/form-step2.html.twig', array(
       'form'      => $form->createView(),
       'prescriber'=> $p,
       'duplicates'=> $duplicates
    ));
}