Php 作为表单集合的Symfony数组

Php 作为表单集合的Symfony数组,php,symfony,Php,Symfony,我在Symfony遇到问题已经好几天了,在谷歌上找不到任何东西 我有两张桌子 账户: AppBundle\Entity\Account: type: entity fields: name: type: string nullable: true length: 255 oneToMany: towns: targetEntity: AppBundle\Entity\Town mappedBy: ow

我在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'];
我想要一个表单,用户可以在其中从数组中选中一个名称(复选框)。当用户选中“纽约”并提交表单时,我希望在
名称
字段中有一个新实体
城镇
,并带有“纽约”。如果用户取消选中“New York”,我希望删除实体

到目前为止,我用
EntityType
CollectionType
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,
            ]
        );
}

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

不要使用数组,将城镇存储在数据库中。
然后将
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\u 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,
            ]
        );
}

对不起,你的问题是什么?您在名称字段中用“纽约”表示“我想要一个新实体”城镇。如果用户取消选中“纽约”,我想要删除该实体”,然后您说“但每次用户提交表单时,它都会添加一个新实体,并且选中任何城镇,并且它确实删除了未选中的城镇”。听起来它完全符合您的要求……但在表单生成器中实例化实体很奇怪。对不起,这是一个输入错误。它不会移除。如果已经存在同名实体,则不应生成新实体。城镇名称数组从何而来?您真的必须使用它吗?它位于问题的标题中,但在form builder示例中不使用它。数组位于“选项”字段中。我通过两个查询创建它。