[英]use multi entity in form builder in symfony2
i want to use more than one entity to create form in createformbuilder . 我想使用多个实体在createformbuilder中创建表单。 forexample i want to have a form with many fields from many entity and i want to check condition for view fields
例如我想拥有一个表单,其中包含来自许多实体的许多字段,并且我想检查视图字段的条件
userEntity -> email , password
resselerEntity - > (userEntity fields) + managerName , managerFamily
leaderEntity - > (userEntity fields) + credit
and if i want to show resseler fields , must show all fields of userEntity
and resselerEntity
如果我想显示resseler字段,则必须显示
userEntity
和resselerEntity
所有字段
if want to show userEntity
, must show all fields of userEntity
如果要显示
userEntity
,则必须显示userEntity
所有字段
and etc, 等等,
so how can i solve this solution ? 那么我该如何解决这个问题呢? Thanks in advance!
提前致谢!
Most common solution is to create single forms for (in your case): 最常见的解决方案是针对以下情况创建单个表单:
Then, create a new form that have two fields of userEntityFormType and ressellerEntityFormType. 然后,创建一个新表单,该表单具有两个字段userEntityFormType和ressellerEntityFormType。
In that way you can: 这样,您可以:
Something like that 像这样
class UserEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstField')
->add('secondField')
[...]
->lastField;
}
public function getName()
{
return 'UserEntityType ';
}
}
class RessellerEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstField')
->add('secondField')
[...]
->lastField;
}
public function getName()
{
return 'RessellersEntityType ';
}
}
class AggregateEntityType extends AbstractType
{
public function BuildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('userEntityField',UserEntityType,array('multiple'=>true)
->add('ressellersEntityField',RessellersEntityType,array('multiple'=>true));
}
public function getName()
{
return 'AggregateEntityType ';
}
}
I think Don got you most of the way there. 我认为唐带给您了大部分帮助。 Add a construct argument to your UserType
将构造参数添加到您的UserType
public function __construct($otherEntityType) // Reseller, Leader etc.
Then use otherEntityType to determine which fields are created for UserType. 然后使用otherEntityType来确定为UserType创建哪些字段。
As far as I understand your problem, you should use type inheritance: 据我了解您的问题,您应该使用类型继承:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('password')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '\Hamid\User',
));
}
public function getName()
{
return 'hamid_user';
}
}
class ResellerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('managerName')
->add('managerFamily')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '\Hamid\Reseller',
));
}
public function getName()
{
return 'hamid_reseller';
}
public function getParent()
{
return 'hamid_user';
}
}
Then use the right form for each entity. 然后为每个实体使用正确的表格。 If, for whatever reason, you need a single form that adapts to the class of the entity set to the form, you need to use form events, as explained in the documentation .
如果出于某种原因,如果您需要一个适合于设置为表单的实体类的表单,则需要使用表单事件,如文档中所述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.