我有一个控制器。我在控制器内有一个表单。我希望从表格(状态表)下拉表单中接收数据,并在单击创建时将从该状态表接收的数据发送到另一个表(站点表)。两个表都在同一个数据库中。那我怎么能在symfony中做到这一点?
控制器
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
));
}
答案 0 :(得分:1)
您应首先创建一个FormType,然后使用EntityType为您下拉,如下所示:
class MyFormType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('type', EntityType::class, [ 'label' => 'State', 'class' => YourStateEntity::class ]); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => YourSiteEntity::class ]); } }
参见" YourStateEntity"和#34; YourSiteEntity"并由您自己的实体替换,这些实体对应于您的状态表和站点表。
然后在你的控制器中:
public function createAction(Request $request, EntityManagerInterface $entityManager) { $site = new YourSiteEntity(); $form = $this->createForm(MyFormType::class, $site); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $entityManager->persist($site); $entityManager->flush(); return $this->redirectToRoute('site_list'); } return $this->render('sites/create.html.twig', [ 'form' => $form->createView(), 'site' => $site ]); }
这是推荐的方法。将所有字段放在" MyFormType"让Symfony做自己的事。
请看这里:https://symfony.com/doc/current/forms.html#creating-form-classes
答案 1 :(得分:0)
Symfony让这种东西成为梦想。
首先,您希望从状态表中获取数据,并为表单正确格式化您想要的位:
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();
}
}
然后在渲染表单时,使用ChoiceType并将其与您的数据结合使用:
// rest of form omitted
->add('state', ChoiceType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px'), 'choices' => $stateChoices));
它会创建一个包含如下选项的选择框:
<option val="NSW">New South Wales </option>
应该工作,我认为:)