Symfony2 - 如何在使用表单类时设置和获取选项? [英] Symfony2 - How to set, and get, options when using a Form Class?

查看:24
本文介绍了Symfony2 - 如何在使用表单类时设置和获取选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用表单类在我的项目中构建各种表单.

I am using Form Classes to build the various form in my project.

在实体类型文件中,对于 buildForm 函数,有一个辅助参数array $options"(这在官方 Symfony 2 文档中显示,但从未提及过!)

In the Entity Type file, for the buildForm function, there is a secondary parameter of "array $options" (this is shown in the official Symfony 2 Documentation, but never mentioned, ever!)

我已经将一个数组输入到控制器的 createForm 函数中,但现在我完全不知道如何检索存储的值?

I have fed an array into the createForm function in my controller but now I am totally stumped on how to retrieve the stored values?

$form = $this->createForm(new ProductType(array(), array('id' => '2')), $product);

我发现获取选项的唯一方法是使用 getOption() 函数,但这在 Symfony 2 中不存在(我发现的帖子是 2010 年的).

The only thing I have found about getting the options is using the getOption() function, but that doesn't exist in Symfony 2 (the post I found was from 2010).

我尝试使用:

$id = $options['id'];

但是当我尝试在任何地方使用 $id 时,出现错误:

But when I try to use $id anywhere, I get the error:

注意:未定义索引:id

Notice: Undefined index: id

是什么?

如何从 $options 数组中检索我的值?我是否一开始就正确设置了它们?

How do I retrieve my values from the $options array? Am I even setting them correctly in the first place?

编辑 - 更多代码:

表单类

<?php

namespace DEMO\DemoBundle\Form\Product;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ProductType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('slug')
            ->add('reference')
            ->add('description')
            ->add('active_from')
            ->add('active_till')
            ->add('is_active')
            ->add('category', 'entity', array(
                'class' => 'DEMO\DemoBundle\Entity\Product\ProductCategory',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->where('u.section = :id')
                        ->setParameter('id', ***ID VARIABLE NEEDS TO GO HERE***)
                        ->orderBy('u.root', 'ASC')
                        ->addOrderBy('u.lft', 'ASC');
                },
                'empty_value' => 'Choose an option',
                'property' => 'indentedName',
            ));
    }

    public function getDefaultOptions()
    {
        return array(
            'data_class' => 'DEMO\DemoBundle\Entity\Product\Product'
        );
    }

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

推荐答案

好吧,事实证明,Gregoires 的回答非常接近,但是在尝试将变量实际放入 createQueryBuilder 函数时却给了我和未定义变量"错误.

Well, it turns out, Gregoires answer was very close, but gave me and "Undefined variable" error when trying to actually but the variable into the createQueryBuilder function.

我花了一段时间试图找出原因并发现了问题.您必须在query_builder"选项中向函数添加一个额外的参数,例如:

I spent a while trying to figure out why and found the issue. You have to add an extra parameter to the function in the "query_builder" option, like such:

'query_builder' => function(EntityRepository $er) use ($options) {
                    return $er->createQueryBuilder('u')
                        ->where('u.section = :id')
                        ->setParameter('id', $options['id'])
                        ->orderBy('u.root', 'ASC')
                        ->addOrderBy('u.lft', 'ASC');
                },

神奇的设置是use ($options)".这允许您在查询生成器中成功使用 $options['id'].

The magic setting being "use ($options)". This allows you to successfully use $options['id'] in the Query Builder.

这篇关于Symfony2 - 如何在使用表单类时设置和获取选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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