Symfony2,如何访问窗体内的实体值? [英] Symfony2, how to access Entity values inside Form?

查看:65
本文介绍了Symfony2,如何访问窗体内的实体值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony2中有一个FormType。用于显示设置。这些设置将作为实体存储在数据库中。使用Doctrine2,我获取设置并创建一个表单,如下所示:

I have a FormType in Symfony2. It is used to display the settings. The settings are stored as entities in a database. Using Doctrine2, I fetch the settings and create a form, like below:

public function showSettingsAction()
{
    if(false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
        throw new AccessDeniedException();
    }
    $settings = new CommunitySettings();

    $repository = $this->getDoctrine()->getRepository('TestTestingBundle:CommunitySettings');
    $allSettings = $repository->findAll();

    $form = $this->createForm('collection', $allSettings, array(
        'type' => 'settings_form'
    ));
    $request = $this->container->get('request');
    if($request->getMethod() === 'POST') {
        $form->bindRequest($request);
        if($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $settings = $form->getData();
            foreach($settings as $setting) {
                $oldsetting = $em->getRepository('TestTestingBundle:CommunitySettings')
                    ->find($setting->getId());
                if(!$oldsetting) {
                    throw $this->createNotFoundException('No setting found for id '.$setting->getId());
                }

                $oldsetting->setSettingValue($setting->getSettingValue());
                $em->flush();
            }

            $this->get('session')->setFlash('message', 'Your changes were saved');

            return new RedirectResponse($this->generateUrl('_admin_settings'));
        }
    }


    return $this->render('TestTestingBundle:Admin:settings.html.twig',array(
        'form' => $form->createView(),
    ));
}

这是我发送<$ c $数组的代码行c> $ allSettings 到 settings_form

$form = $this->createForm('collection', $allSettings, array(
    'type' => 'settings_form'
));

设置表的外观如下:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('settingValue', 'text');
}

我在实体中存储了一个标签,一个值和一个字段类型,我想用那些来构建表格。但是,当我使用它时,它仅显示表单中的变量名称,如下所示:

I have a label, a value and a field type stored in the entity and I would like to use those for building the form. However, when I use this it only shows me the variable names in the Form, like this:

0
Settingvalue //Is a checkbox, where it says Settingvalue, it should be the label stored in the entity
0

1
Settingvalue //Is a integer, where it says Settingvalue, it should be the label stored in the entity
3000

如何使用存储的变量

推荐答案

您可以在设置表单类型中使用事件侦听器来解决此问题吗?

You can use an event listener in your settings form type to solve this problem.

public function buildForm(FormBuilder $builder, array $options)
{
    $formFactory = $builder->getFormFactory();
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
        $form = $event->getForm();
        $data = $event->getData();

        $form->add($formFactory->createNamed('settingsValue', $data->getSettingsType(), array(
            'label' => $data->getSettingsLabel(),
        )));
    });
}

这篇关于Symfony2,如何访问窗体内的实体值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆