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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失