drau89457 2017-10-27 11:52
浏览 41
已采纳

如何在symfony3中访问同一表单中的2个表?

I have a controller. I have a form inside that controller. I want to receive data in the form as drop down from a table(states table) and send data received from that states table to another table(site table) when I click create. Both tables are in the same database. So how can I do it in symfony?

Controller

public function createAction(Request $request)
    {

        $site = new site;

        $form = $this->createFormBuilder($site)




    ->add('site_id', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('site_type', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('company', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('main_company', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('site_name', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('unit_no', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('street_no', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('street', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('suburb', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('state', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('post_code', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('contact_no', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('fax', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('access_notes', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('e_mail', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('web', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('contact_person_1', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('contract_starting_date', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('contract_end_date', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('contract_special_note', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('site_specification', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('other_comments', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('added_by', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))

    ->add('hours_per_month', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('cleaners_per_session', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('auto_sign_out', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('time_zone', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('location_path', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('lat', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('lng', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('service_price', NumberType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('earliest_start', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('access_duration', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('time_span', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('cost_rates_json', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
    ->add('site_status', IntegerType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))









    // ->add('date_time', DateTimeType::class, ['attr' => ['style' => 'margin-bottom: 15px;display: flex']])
    ->add('save', SubmitType::class, array('label' => "Create",'attr' => array('class' => 'btn btn-success', 'style' => 'margin-bottom:15px')))
    ->getForm();









        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid())





        {




//Get Data

            $site_id = $form['site_id']->getData();
            $site_type = $form['site_type']->getData();
            $category = $form['category']->getData();
            $company = $form['company']->getData();
            $main_company = $form['main_company']->getData();
            $site_name = $form['site_name']->getData();
            $unit_no = $form['unit_no']->getData();
            $street_no = $form['street_no']->getData();
            $street = $form['street']->getData();
            $suburb = $form['suburb']->getData();
            $state = $form['state']->getData();
            $post_code = $form['post_code']->getData();
            $contact_no = $form['contact_no']->getData();
            $fax = $form['fax']->getData();
            $access_notes = $form['access_notes']->getData();
            $e_mail = $form['e_mail']->getData();
            $web = $form['web']->getData();
            $contact_person_1 = $form['contact_person_1']->getData();
            $contract_starting_date = $form['contract_starting_date']->getData();
            $contract_end_date = $form['contract_end_date']->getData();
            $contract_special_note = $form['contract_special_note']->getData();
            $site_specification = $form['site_specification']->getData();
            $other_comments = $form['other_comments']->getData();
            $added_by = $form['added_by']->getData();
            $hours_per_month = $form['hours_per_month']->getData();
            $cleaners_per_session = $form['cleaners_per_session']->getData();
            $auto_sign_out = $form['auto_sign_out']->getData();
            $time_zone = $form['time_zone']->getData();
            $location_path = $form['location_path']->getData();
            $lat = $form['lat']->getData();
            $lng = $form['lng']->getData();
            $service_price = $form['service_price']->getData();
            $earliest_start = $form['earliest_start']->getData();
            $access_duration = $form['access_duration']->getData();
            $time_span = $form['time_span']->getData();
            $cost_rates_json = $form['cost_rates_json']->getData();
            $site_status = $form['site_status']->getData();
            $now = new\DateTime('now');






            $site->setSiteId($site_id);
            $site->setSiteType($site_type);
            $site->setCompany($company);
            $site->setMainCompany($main_company);
            $site->setSiteName($site_name);
            $site->setUnitNo($unit_no);
            $site->setStreetNo($street_no);
            $site->setStreet($street);
            $site->setSuburb($suburb);
            $site->setCategory($category);
            $site->setState($state);
            $site->setPostCode($post_code);
            $site->setContactNo($contact_no);
            $site->setFax($fax);
            $site->setAccessNotes($access_notes);
            $site->setEMail($e_mail);
            $site->setWeb($web);
            $site->setContactPerson1($contact_person_1);
            $site->setContractStartingDate($contract_starting_date);
            $site->setContractEndDate($contract_end_date);
            $site->setContractSpecialNote($contract_special_note);
            $site->setSiteSpecification($site_specification);
            $site->setOtherComments($other_comments);
            $site->setAddedBy($added_by);
            $site->setHoursPerMonth($hours_per_month);
            $site->setCleanersPerSession($cleaners_per_session);
            $site->setAutoSignOut($auto_sign_out);
            $site->setTimeZone($time_zone);
            $site->setLocationPath($location_path);
            $site->setLat($lat);
            $site->setLng($lng);
            $site->setServicePrice($service_price);
            $site->setEarliestStart($earliest_start);
            $site->setAccessDuration($access_duration);
            $site->setTimeSpan($time_span);
            $site->setCostRatesJson($cost_rates_json);
            $site->setSiteStatus($site_status);
            $site->setDateTime($now);



            $em = $this->getDoctrine()->getManager();
            $em->persist($site);
            $em->flush();









            $this->addFlash(
                    'notice',
                    'Entry Created'
                );











//return new RedirectResponse('http://localhost:8000/loc/');




 return $this->redirectToRoute('site_list');


        }






         return $this->render('sites/create.html.twig', array(
                'form' => $form->createView(),
                'site' =>  $site
            ));
    }

I want dropdown from this table go to the site_table in the category field like this(I hardcoded the site table)

  • 写回答

2条回答 默认 最新

  • douyan4470 2017-10-27 13:29
    关注

    Symfony makes this sorta stuff a dream.

    First you want to get the data from your states table and format the bits you want correctly for the form:

    public function createAction(Request $request) {
         $statesRepo = $this->getDoctrine->getManager()->getRepository('AppBundle:States'); // assuming here the name of the states repo, you might need to change this
         $states = $statesRepo->findAll();
         $stateChoices = []; 
         foreach ($states as $state) {
            $stateChoices[$state->getName()] = $state->getStateCode(); 
         }
    
    }
    

    Then when rendering the form, use ChoiceType and combine it with your data:

    // rest of form omitted
    ->add('state', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $stateChoices));
    

    It will create a select box with options like so:

    <option val="NSW">New South Wales </option>
    

    Should work, i think :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决