dptn69182 2016-05-10 20:44
浏览 47
已采纳

如何在Symfony 3中将表单变量从控制器动作传递给另一个

I'm using Symfony 3 trying to develop a website that manage students absence. When a user want to see absences he must give a student name so that he can see absences for this specific student. The user gives the student name in a form ( combobox containing a list of students)

I'm trying to use this form variable ( the name) to display absences for the student with this name.

This is my code.

In this first action i'm creating the form where the user will chose the student name and i recover the submitted value to get the ID of the student and i want to pass this id to the second action where i will fetch the absences using the id and display it in the twig page :

/**
* @Route("/absence/list/bystudent", name = "list_absence_student")
*/

public function listabsencestudentAction(Request $request)
{
    $absence = new Absence();
    $form   = $this->get('form.factory')->create(AbsenceStudentType::class, $absence);


    if($request->getMethod() == 'POST'){
        $form->handleRequest($request);
        $data = $form->getData();
    }
    if($form->isValid()){
        $id = $data->getStudent()->getID();

        return $this->redirectToRoute('list_absence_student_show',array('id', $id));
    }

    return $this->render('AbsenceBundle:Default:listabsencestudent.html.twig', array('f'=> $form->createView()));
}

And this is the Action where i'm trying to fetch the absences using the id of the previous Action

 /**
  * @Route("/absence/list/bystudent/show", name = "list_absence_student_show")
 */

public function listabsencestudentshowAction()
{
 $absences = $this->getDoctrine()->getRepository("AbsenceBundle:Absence")->findBy('student' => $id);
           return $this->render('AbsenceBundle:Default:listabsencestudentshow.html.twig', array('absences' => $absences));
}

I have a relation ManyToOne between the entities Absence and Student so i have a field named student in my Absence entity that why im using the student field in the findBy() Method.

The problem is that the variable $id is not recognize in the listabsencestudentshowAction() i don't know how to pass this form variable from an action to another

Can anyone help me with this please.

  • 写回答

1条回答 默认 最新

  • dota220141003 2016-05-13 00:18
    关注

    I found a solution to my problem & i want to share it maybe someone will need it

    in order to recover the attributes that i passed i needed to add a slug in the rout of the second controller action then add arguments to this action

    this is the code

    nothing changed in the first Action

    /**
    * @Route("/absence/list/bystudent", name = "list_absence_student")
    */
    
    public function listabsencestudentAction(Request $request)
    {
        $absence = new Absence();
        $form   = $this->get('form.factory')->create(AbsenceStudentType::class, $absence);
    
    
    if($request->getMethod() == 'POST'){
        $form->handleRequest($request);
        $data = $form->getData();
    }
    if($form->isValid()){
        $id = $data->getStudent()->getID();
    
        return $this->redirectToRoute('list_absence_student_show',array('id', $id));
    }
    
    return $this->render('AbsenceBundle:Default:listabsencestudent.html.twig', array('f'=> $form->createView()));
    

    }

    And this is the second bundle

    /**
     * @Route("/absence/list/bystudent/show/{id}", name = "list_absence_student_show")
     */
    
    public function listabsencestudentshowAction(Request $request, $id)
    {
        $absences = $this->getDoctrine()->getRepository("AbsenceBundle:Absence")->findBy(array('student' => $id));
        return $this->render('AbsenceBundle:Default:listabsencestudentshow.html.twig', array('absences' => $absences, 'id' => $id));
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵