提交后如何仅获取更改的实体?

时间:2019-02-07 12:55:56

标签: symfony symfony-forms symfony4 formbuilder

我在下面的代码中有a collection of forms,它导致了如下所示的形式(带有两个示例条目):

Just like this

单击提交时,我提供给表单(构建器)实例的数据将相应地更新。

问题:我遇到的问题是,列表可能很长,所以我需要一种方法来知道已更新哪些实例。

我考虑过要在会议中存储原始数据的副本(此处为$leadPartnerList。但这感觉不对。

symfony(特别是表单生成器)是否提供开箱即用的功能?还是知道该表单中的哪些字段已更新而哪些未更新的有效解决方案?

我的树枝:

{% block content %}

<div>

    {{ form_start(form) }}
    {% for partner in lead_partners %}
        {{ form_row(partner.name) }}
    {% endfor %}
    {{ form_end(form) }}

</div>

{% endblock content %}

我的控制器代码:

public function overview(Request $request, \App\Utility\LeadPartnerLoader $LeadPartnerLoader)
{
    $leadPartnerList = $leadPartnerLoader->loadAll();
    $formBuilderData = [
        'lead_partners' => $leadPartnerList
    ];

    $listForm = $formFactory->createNamedBuilder('listForm', FormType::class, $formBuilderData)
        ->add('lead_partners', CollectionType::class, [
            'entry_type' => LeadPartnerFormType::class,
            'allow_add' => true
        ])
        ->add('submit', SubmitType::class, [
            'label' => 'Submit Changes'
        ])
        ->getForm();

    ... handleRequest and so on and so forth...
}

以及表单类型(LeadPartnerFormType):

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

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class);
}

$leadPartnerListarray类型,在数组的每个条目中都有LeadPartner实例。

请注意:我在这里不使用教义!

1 个答案:

答案 0 :(得分:-1)

使用symfony EventListener或EventSubscriber。参见:https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html