我无法从 EntityType 获取值。我的最新版本是 3.3.6。
class BuildType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder
->add('title', TextType::class)
->add('save', SubmitType::class, array('label' => 'Create Post'))
->add('team', CollectionType::class, array(
// each entry in the array will be an "email" field
'entry_type' => TeamType::class,
// these options are passed to each "email" type
'entry_options' => array(
'attr' => array('class' => 'form-control'),
),
'label' => false,
'allow_add' => true,
'prototype' => true,
'mapped' => false
));
}
}
class TeamType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder
->add('name', EntityType::class, array(
'placeholder' => 'Choice a champ',
'required' => true,
'class' => 'AppBundle:Champions',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
},
'choice_label' => 'name',
'choice_value' => 'id',
'attr' => array('class' => 'dropdown'),
));
}
我尝试了所有方法,但无法获取 TeamType 的“名称”值。提交表格后,我这样做
foreach ($form["team"]->getData() as $value) {
'name' => $value['name']
但该值为空。如果我尝试转储请求并且该值就在那里。我可以获取其他值并将其保存在数据库中。只有 EntityType i 不能。 有人知道怎么办吗?
最佳答案
EntityType 作为对象返回。您应该使用模型 getter 函数。
$form->get('name')->getData()->getId(); // getName() vs..
这是一个类似的示例。
关于php - 从 EntityType Symfony 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46471611/