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 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么