Symonfy表单类型:将实体字段选项限制为关联的值 [英] Symonfy form type: restrict entity field choices to associated values

查看:157
本文介绍了Symonfy表单类型:将实体字段选项限制为关联的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体 SearchFieldType ,并带有 ManyToMany SearchOperator

  / ** 
* @ ORM \ManyToMany(targetEntity =SearchOperator,cascade = { $ persistence,remove})
* @ ORM\JoinTable(
* joinColumns = {@ ORM\JoinColumn(name =type_id,referencedColumnName =id)},
* inverseJoinColumns = {@ ORM \JoinColumn(name =operator_id,referencedColumnName =id)}
*)
** /
private $ operators;

在表单类型中,默认设置显示一个带有全部的选择控件现有的运营商选择,而我只想显示该实体当前可用的运营商。这是我的(失败)尝试,因为我读了我需要创建一个事件监听器(是吗?)以访问关联的实体:

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

if($ data!= null)
{
$ form
- > add( ($ formFactory-> createNamed('name','text',array('auto_initialize'=> false)))
- > add($ formFactory-> createNamed('operators','entity' ,array('class'=>'AppBundle:SearchOperator',
'multiple'=> false,
'expanded'=> false,
'choices'=> $ data-> getOperators())))
;
}
});
}

我收到此错误:


自动初始化仅在根窗体上受支持。您应该在name字段中将auto_initialize选项设置为false。

我尝试将此选项设置为false字段和表单本身( setDefaultOptions )没有结果。 我目前的symfony版本是2.7.6

解决方案

SearchFieldType中的 $ operators 您已映射了多对多关系。我假设下面的表单是用于 SearchFieldType 的。

因为你有一个ManyToMany关系,这意味着你的表单期望在'操作员'的输入中有许多值。但是,您将字段类型设置为实体。这些不兼容。如果,如你所说你只想要一个下拉列表,那么你可能想要将关系改变为ManyToOne。 (如果您想保留ManyToMany,您必须将表单类型设置为'collection')。



您有另一个错误:


自动初始化仅在根表单上受支持。您应该在name字段中将auto_initialize选项设置为false。

通过从字段中删除此选项'name',据我所知:
array('auto_initialize'=> false



然后,在将你的关系修复为ManyToOne之后,你可以通过使用 query_builder 来为查询传递一个QueryBuilder实例和一些查询过滤器(我甚至不理解你想要过滤的注释) / code>实体字段类型的选项(请参阅此处)。


I have an entity SearchFieldType with a ManyToMany to SearchOperator:

/**
 * @ORM\ManyToMany(targetEntity="SearchOperator", cascade={"persist", "remove"})
 * @ORM\JoinTable(
 *      joinColumns={@ORM\JoinColumn(name="type_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="operator_id", referencedColumnName="id")}
 *      )
 **/
 private $operators;

In the form type, the default setup show a select control with all existing operators to choose, while I'd like to show only the current available operators for that entity. Here's my (failed) attempt, as I read I need to create an event listener (do I?) in order to access the associated entity:

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

        if ($data != null)
        {
            $form
                ->add($formFactory->createNamed('name', 'text', array('auto_initialize' => false)))
                ->add($formFactory->createNamed('operators', 'entity', array('class' => 'AppBundle:SearchOperator',
                                                   'multiple' => false,
                                                   'expanded' => false,
                                                   'choices'  => $data->getOperators())))
            ;
        }
      });
}

I receive this error:

Automatic initialization is only supported on root forms. You should set the "auto_initialize" option to false on the field "name".

I tryed to set this option to false in the field and in the form itself (setDefaultOptions) with no result.

My current symfony version is 2.7.6

解决方案

On the $operators in the SearchFieldType you have mapped a many to many relationship. I assume the form underneath is for SearchFieldType.

Because you have a ManyToMany relationship this means your form expects to have many values in the inputs for 'operators'. However you set the field type to 'entity'. These are not compatible. If, as you said you only want a drop down list then you might want to change the relationship to be ManyToOne. (If you want to keep the ManyToMany you have to set the form type to 'collection').

The other error you have:

Automatic initialization is only supported on root forms. You should set the "auto_initialize" option to false on the field "name".

Will be solved by removing this option from the field 'name' as far as I can tell: array('auto_initialize' => false

Then, after you fix your relation to be ManyToOne you can pass a QueryBuilder instance with some filters for the query (I did not understand even form the comments what you want to filter by) by using the query_builder option for the 'entity' field type (see here).

这篇关于Symonfy表单类型:将实体字段选项限制为关联的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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