symfony2 实体选择标签显示 [英] symfony2 Entity select tag display

查看:31
本文介绍了symfony2 实体选择标签显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有 ManyToMany 关系的 Symfony 表单工作正常,它在实体 Party 上显示所有具有 name 属性的各方.

I have this Symfony form with a ManyToMany relation working fine, it displays all the parties with the property name on the entity Party.

提交时,根据被选中的参与方查询数据库,检索被邀请到参与方的人员.

When submitted, it queries the database according to the parties that are selected and retrieves the persons that are invited to those parties.

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('parties', 'entity', array(
                'class' => 'ProtoBundle:Party',
                'multiple' => true,
                'expanded' => false,
                'property' => 'name',
                'required' => false,));
}

带参数

'multiple' => 'true,

所有各方同时显示在一个选择下拉框中(不是我想要的).

all the parties are displayed at the same time in a select drop down box (not what i want).

我想要的只是一个带有参数的选择标签

What I want is just to have one select tag with parameter

'empty_value' => 'choose a party'

,然后当用户点击它时,值就会显示出来.其实我可以用参数来做到这一点

, then when the user clicks on it, the values are displayed. Actually I can do this with the parameter

'multiple'=> false,

但问题是我收到此错误消息:

but the problem is that I get this error message:

属性partys"和方法setParties()"、__set()"或__call()"都不存在并且在类Acme\ProtoBundle\Entity\Person"中具有公共访问权限.

Neither the property "parties" nor one of the methods "setParties()", "__set()" or "__call()" exist and have public access in class "Acme\ProtoBundle\Entity\Person".

有谁知道如何使这个 select 标签起作用并给我一个详细的解决方案?

Does anyone knows how to make this select tag working and bring me a detailed solution?

推荐答案

首先你应该考虑,如果你真的需要多对多的关系,当你想要没有多选的简单选择框时.

In first of all you should get in consideration that if you really need many to many relation when you want simple select box without multiple select.

可是……

在实体中,您必须检查传入的值是否为数组,仅此而已:

in entity you will have to check if comming value is array, and that's it:

public function setParties($parties) 
{
    if (!is_array($parties)) {
        $parties = array($parties);
    }
    $this->parties = $parties;
}

这篇关于symfony2 实体选择标签显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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