[英]Symfony 2 Create a entity form field with 2 properties
I am using symfony2 and have a form to save the relation of one user to some rules.我正在使用symfony2并有一个表单来保存一个用户与某些规则的关系。 These rules are set by the admin user of the company.
这些规则由公司的管理员用户设置。 In this form, after I selected a user to update, I have to select which rule this user have permission.
在这个表单中,在我选择了一个用户进行更新后,我必须选择这个用户有权限的规则。
The problem is that I may have more then one rule with the same name (it's another entity) but the values are different.问题是我可能有多个具有相同名称的规则(它是另一个实体),但值不同。 So, when I build the selectbox I must show the name and the value like:
因此,当我构建选择框时,我必须显示名称和值,例如:
But now I just can show without the "- $value" using the code bellow:但是现在我可以使用下面的代码在没有“- $value”的情况下显示:
$form = $this->createFormBuilder()->add('myinput', 'entity', array(
'class' => 'myBundle:Rule',
'property' => 'childEntity.name',
'label' => 'Filas Permitidas',
'expanded' => false,
'multiple' => true,
'choices' => $this->getDoctrine()
->getRepository('MyBundle:Rule')
->findAll(),
'required' => true,
))->getForm();
So, as property I wanted to get $myEntity->getChildEntity()->getName()
and the $myEntity->getValue()
.因此,作为属性,我想获得
$myEntity->getChildEntity()->getName()
和$myEntity->getValue()
。
Is there some way to do this?有没有办法做到这一点?
Yes, define a getUniqueName()
method in the entity class like:是的,在实体类中定义一个
getUniqueName()
方法,例如:
public function getUniqueName()
{
return sprintf('%s - %s', $this->name, $this->value);
}
And edit the property
form option:并编辑
property
表单选项:
'property' => 'childEntity.uniqueName',
You also can omit the property
option and define the __toString()
method same way in order to not repeat the setting of the property
option in every form.您也可以省略
property
选项并以相同的方式定义__toString()
方法,以免在每种形式中重复设置property
选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.