2017-12-05 1 views
0

데이터베이스에 새 처방집을 추가 할 수있는 양식이 있습니다. 첫 번째 단계는 처방 자에 대한 다양한 정보를 제공하는 것입니다.Symfony3 : 다단계 양식 및 전달 후 플러시

그런 다음 비슷한 처방약이 있는지 확인한 후 추가합니다 (두 번째 양식의 두 번째 단계).있을 경우 사용자에게 확인을 요청합니다.

요약하면 중복에 따라 1 단계 양식 또는 2 단계 양식이 있습니다.

CraueFormFlowBundle을 사용해 보았지만 조건부 두 번째 단계를 구현하는 방법을 모르겠습니다. 내 테스트는 결정적이지 않았습니다. 그래서 컨트롤러에서 forward 메서드를 사용하기로 결정했습니다.

그러나, 나는 (전송 후) 2 단계의 끝에서 내 처방을 플러시 관리 할 수 ​​없습니다,이 오류가 있습니다 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 
)); 
} 

나는 문제가 F 온다 생각 ROM에 2 개의 양식 제출 사실이 있습니다.

+0

를 들어

이 답변으로 작성하고 대답으로 표시하십시오. –

답변

0

세션을 사용하여 해결책을 찾았습니다. (나는 그것이 완벽한 방법은 아니다 알고하지만 난 다른 하나를 찾을 수 없습니다) 심포니 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 
    )); 
} 
+0

어떻게 뒤로 버튼을 처리 했습니까? – utkarsh2k2