首页 > 解决方案 > Symfony 数组作为表单集合

问题描述

我在 Symfony 中遇到问题好几天了,在谷歌上找不到任何东西。

我有两张桌子。

帐户:

AppBundle\Entity\Account:
type: entity
fields:
    name:
        type: string
        nullable: true
        length: 255
oneToMany:
    towns:
        targetEntity: AppBundle\Entity\Town
        mappedBy: owner
        cascade: ['persist']

镇:

AppBundle\Entity\Town:
type: entity
fields:
    name:
        type: string
        nullable: true
        length: 255
manyToOne:
    owner:
        targetEntity: AppBundle\Entity\Account
        inversedBy: towns

我有一个名称数组:

$names = ['New York','Berlin'];

我想要一个表单,用户可以在其中检查数组中的名称(复选框)。当用户检查“纽约”并提交表单时,我想要一个在字段Town中带有“纽约”的新实体。name如果用户取消选中“纽约”,我希望删除该实体。

到目前为止,我尝试使用EntityType,CollectionTypeChoiceType. 我能得到的最好的就是 ChoiceType。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('towns', ChoiceType::class,
            [
                'choices' =>
                    [
                        new Town('New York'),
                        new Town('Berlin'),
                    ],
                'choice_label' => function (Town $town, $key, $index) {
                    return $town->getName();
                },
                'choice_value' => function (Town $town) {
                    return $town->getName();
                },
                'expanded' => TRUE,
                'multiple' => TRUE,
                'required' => FALSE,
            ]
        );
}

但是,每次用户提交任何已选中城镇的表单时,它都会添加一个新实体,并且不会删除未选中的城镇......

标签: phpsymfony

解决方案


不要使用数组,将城镇存储在数据库中。
然后将towns字段类型设置为EntityType

->add(
    'towns',
    EntityType::class,
    [
        'class' => Town::class,
        'multiple' => true,
        'expanded' => true,
        'required' => false,
    ]
)

将此方法添加到您的 Town 类中,以便在必须将其转换为字符串的任何地方自动显示名称:

/**
 * @return string
 */
public function __toString()
{
    return $this->name;
}

如果数组的想法是过滤显示的选项以选择城镇,您可以在query_builder选项中使用它:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $names = ['New York', 'Berlin']; // Or pass it from the controller in $options

    $builder
        ->add('towns',
            EntityType::class,
            [
                'class' => Town::class,
                'query_builder' => function (EntityRepository $er) use ($names) {
                    return $er->createQueryBuilder('town')
                        ->where('town.name IN (:names)')
                        ->setParameter('names', $names);
                },
                'multiple' => true,
                'expanded' => true,
                'required' => false,
            ]
        );
}

推荐阅读