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 go to the site_table in the category field (I hardcoded the site table)