2017-01-06 119 views
0

我有一個簡單的表格,其中包含一些(< 10)行的3列。使用數據庫中的數據創建Symfony/Twig表格

表:

/** 
* @ORM\Entity 
* @ORM\Table(name="property_addition_cost_frequency") 
*/ 
class PropertyAdditionCostFrequency 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    */private $id; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    private $label; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    private $group; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    private $active; 
    //all getters and setters 
} 

我有用於示出在預填充形式1行一個工作碼。

AdminCoreDataController.php

/** 
* @Route("/core_data/{id}", defaults={"id" = 1}, name="admin_core_data") 
*/ 
public function showCoreDataListAction(Request $request, PropertyAdditionCostFrequency $propertyAdditionCostFrequency) 
{ 
    $form = $this->createForm(PropertyAdditionCostFrequencyForm::class, $propertyAdditionCostFrequency); 
    $form->handleRequest($request); 

    if($form->isSubmitted() && $form->isValid()){ 
     $propertyAdditionCostFrequency = $form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($propertyAdditionCostFrequency); 
     $em->flush(); 
     $this->addFlash('success', 'successfully changed the data'); 
     return $this->redirectToRoute('admin_property_list'); 
    } 

    return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [ 
     'propertyCostFrequencyForm' => $form->createView() 
    ]); 
} 

core_data.html.twig

{{ form_start(propertyCostFrequencyForm) }} 
    {{ form_widget(propertyCostFrequencyForm.label) }} 
    {{ form_widget(propertyCostFrequencyForm.active) }} 
    <button type="submit">send</button> 
{{ form_end(propertyCostFrequencyForm) }} 

PropertyAdditionCostFrequencyForm.php

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add( 'label') 
     ->add( 'active') 
    ; 
} 

我怎樣才能得到一個形式,從如果表中的所有行不「id」是給定的?

+3

開始與形式的集合:http://symfony.com/doc/current/form/form_collections.html – Cerad

+0

如果我理解正確的,你要編輯的所有PropertyAdditionCostFrequency在同一時間?或者你想要10個表單,每行一個? – goto

+0

@goto是的,我想要以一種形式同時編輯所有行 – bob

回答

1

塞拉德是對的,symfony.com/doc/current/form/form_collections.html是一個很好的開始。謝謝! 一些改變,我得到

表中的代碼後:

class PropertyAdditionCostFrequency 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    */private $id; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    private $label; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    private $group; 

    /** 
    * @ORM\Column(type="boolean") 
    */ 
    private $active; 

    //getter and setter 
} 

AdminCoreDataController.php

public function showCoreDataListAction(Request $request) 
{ 
    $repository = $this->getDoctrine()->getRepository('AppBundle:PropertyAdditionCostFrequency'); 
    $cost_frequency = $repository->findAll(); 

    $PropertyAdditionCostFrequency = new PropertyAdditionCostFrequency(); 

    foreach ($cost_frequency as $k => $v) { 
     $PropertyAdditionCostFrequency->getLabel()->add($v); 
    } 

    $form = $this->createForm(PropertyAdditionCostFrequencyForm::class, $PropertyAdditionCostFrequency); 
    $form->handleRequest($request); 

    if($form->isSubmitted() && $form->isValid()){ 
     $em = $this->getDoctrine()->getManager(); 
     $em->flush(); 
     $this->addFlash('success', 'successfully changed the data'); 
     return $this->redirectToRoute('admin_core_data'); 
    } 

    return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [ 
     'propertyCostFrequencyForm' => $form->createView() 
    ]); 
} 

core_data.html.twig

{{ form_start(propertyCostFrequencyForm) }} 
    {% for single_frequency in propertyCostFrequencyForm.label %} 
     <div class="row"> 
      <div class="col-sm-4"> 
       {{ form_widget(single_frequency.label) }} 
      </div> 
      <div class="col-sm-1"> 
       {{ form_widget(single_frequency.active) }} 
      </div> 
     </div> 
    {% endfor %} 
<button type="submit">send</button> 
{{ form_end(propertyCostFrequencyForm) }} 

PropertyAdditionCostFrequencyForm.php

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add( 'label', CollectionType::class, array(
      'entry_type' => PropertyAdditionCostFrequencyType::class 
     )) 
     ->add( 'active', CollectionType::class, array(
      'entry_type' => PropertyAdditionCostFrequencyType::class 
     )) 
    ; 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults([ 
     'data_class' => PropertyAdditionCostFrequency::class 
    ]); 
} 

PropertyAdditionCostFrequencyType.php

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('label') 
     ->add('active', CheckboxType::class, array(
      'required' => false, 
     )) 
    ; 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => PropertyAdditionCostFrequency::class, 
    )); 
}