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 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿