使用数据库中的数据创建Symfony / Twig表单

时间:2017-01-06 14:59:23

标签: symfony

我有一个简单的表,其中包含3列,其中包含一些(< 10)行。

表:

/**
 * @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()
    ]);
}

我有一个工作代码,用于以预先填充的形式显示1行。

AdminCoreDataController.php

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

core_data.html.twig

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

PropertyAdditionCostFrequencyForm.php

{{1}}

如果没有给出“id”,如何从表中获取包含所有行的表单?

1 个答案:

答案 0 :(得分:1)

Cerad是对的,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,
    ));
}