gpt4 book ai didi

php - 具有不同字段的 Symfony CollectionType

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:33 27 4
gpt4 key购买 nike

问题:

考虑以下订单表单有很多要求:

Title:       [_________________]

REQUIREMENTS:

What sizes? [X] Small [X] Medium [_] Large
What shapes? [_] Circle [X] Square [_] Triangle
What colors? [X] Red [_] Green [X] Blue
.
.
.

如何在 Symfony 3.2 中生成和处理表单?

我怎么想的:

[订单] ------OneToMany------ [要求] ------OneToMany------ [选择]

订单类型

class OrderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form = $builder
->add('title', TextType::class, array());
->add('requirements', CollectionType::class,
array(
'entry_type' => RequirementType::class
)
)
->add('submit', SubmitType::class, array(();

return $form;
}
}

问题

我不知道如何写RequirementType,因为它们不完全相同(大小形状, 颜色, ...).

这是我的想法:

需求类型

class RequirementType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$form = $builder
->add(??????, EntityType::class,
array(
'label' => ??????,
'expanded' => true,
'multiple' => true,
'class' => Selection::class,
'query_builder' => call_user_func(function (EntityRepository $er, $requirement) {
return $er->createQueryBuilder('s')
->where('s.requirement = :requirement')
->setParameter('requirement', $requirement)
},$em->getRepository($args['class']), $requirement);
)
);

return $form;
}
}

最佳答案

如果我理解正确,需求的属性(“小”、“中”、“大”……)存储在 Collection 表中,并且与需求相关(“尺寸”、“形状”、“颜色” ” …) 具有 oneToMany 关系(一个需求可以有多个选择)….

如果是这样,以下代码有效:

OrderType.php

$builder
->add('requirements', CollectionType::class,
array(
'entry_type' => RequirementType::class
)
);

RequirementType.php :

$builder
->add('name', HiddenType::class, array('disabled'=>true))
->add('collections', EntityType::class, array(
'class' => ‘AppBundle:Collection’,
'choice_label' => 'name', 'multiple' =>true))

在您的 Twig View 中:

{{ form_start(orderForm) }}
{% for requirement in orderForm.requirements %}
<label>{{ requirement.name.vars.value }}</label>
{{ form_widget(requirement.collections) }}
{{ form_widget(requirement.name) }}
<br>
{% endfor %}
{{ form_end(orderForm) }}

关于php - 具有不同字段的 Symfony CollectionType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41315729/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com