如何在Symfony2中使用一堆不相关的实体创建表单? [英] How do I make a form with a bunch of unrelated entities in Symfony2?

查看:99
本文介绍了如何在Symfony2中使用一堆不相关的实体创建表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个日志解析器,该解析器使用数据库表将日志事件翻译"为人类可读的等价物以进行报告.

I'm working on a log parser that uses a database table to "translate" log events into human-readable equivalents for reporting.

例如,类似"start_application_minecraft" 的日志条目将转换为已启动的Minecraft" .

For example, a log entry like "start_application_minecraft" would get converted to "Started Minecraft".

我正在尝试创建一个用于添加/更新显示文本的Web界面,但我不知道如何将它们放入 Symfony Form对象.

I'm trying to make a web interface for adding/updating the display text, but I can't figure out how to get them into a Symfony Form object.

我有一个LogEvent实体(具有IDTextDisplayText的属性),并且我创建了与这些属性相对应的表单类型.

I have a LogEvent entity (with properties for ID, Text, and DisplayText), and I've created a Form Type that corresponds to these properties.

它一次可以修改一个事件,效果很好,但我想将它们全部都放在一个页面上,并用一个提交"按钮来更新所有内容.问题是我在嵌入表单时可以找到的所有文档都处理了相关的实体(例如,包含多个产品的类别),但是在我的情况下,我需要使用的所有实体都是完全不相关的.进行此设置的最佳方法是什么?

It works fine for modifying one event at a time, but I'd like to have them all on one page with a single Submit button to update everything. The problem is that all the documentation I can find on embedding Forms deals with entities that are related (e.g. a Category containing multiple Products), but in my case all of the entities I need to work with are completely unrelated. What's the best way to go about setting this up?

推荐答案

使用Symfony集合"表单字段类型,并使用Doctrine查找所需的LogEvent实体并将其传递给集合.

Use a Symfony 'Collection' form field type and use Doctrine to find the LogEvent entities you want and pass that to the Collection.

示例: http://symfony.com/doc/current/cookbook/form/form_collections.html

参考: http://symfony.com/doc/current/reference/forms/types/collection.html

因此,首先您将使LogEvent表单类型为:

So, first you would make your LogEvent form type:

class LogEventType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('text');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\Bundle\Entity\LogEvent',
        ));
    }

    public function getName()
    {
        return 'log_event';
    }
}

然后将您的表单类型保存为LogEvent实体的集合:

Then make your form type that holds the Collection of LogEvent entities:

class MultiLogEventType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'logEvents', 'collection', array('type' => new LogEventType())
        );
    }

    public function getName()
    {
        return 'multi_log_event';
    }
}

然后在您的Controller中,创建表单并将您的日志事件传递给它:

Then in your Controller, create the form and pass your log events to it:

public function indexAction()
{
    // replace findAll() with a more restrictive query if you need to
    $logEvents = $this->getDoctrine()->getManager()
        ->getRepository('MyBundle:LogEvent')->findAll();

    $form = $this->createForm(
        new MultiLogEventType(),
        array('logEvents' => $logEvents)
    );

    return array('form' => $form->createView());
}

然后,在您的编辑操作中,您可以遍历日志事件并执行所需的任何操作:

Then in your edit action you can loop through the log events and do whatever you need to:

public function editAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $editForm = $this->createForm(new MultiLogEventType());
    $editForm->handleRequest($request);

    if ($editForm->isValid())
    {
        foreach ($logEvents as $logEvent) {
            // perform any logic you need to here
            // (ex: removing the log event; $em->remove($logEvent);)
        }
        $em->flush();
    }

    return $this->redirect($this->generateUrl('log_event_edit'));
}

这篇关于如何在Symfony2中使用一堆不相关的实体创建表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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