Symfony 3.4表单字段名称和带有选项indexBy的实体之间发生冲突

时间:2019-10-10 15:08:37

标签: symfony doctrine-orm symfony-3.4

我正在尝试制作表格,但似乎是由OneToMany注释的选项indexBy =“ label”

引起的冲突。
  

名称“示例frais”包含非法字符。名称应以字母,数字或下划线开头,并且只能包含字母,数字,数字,下划线(“ _”),连字符(“-”)和冒号(“:”)。

“ Exeple frais”是我实体的标签。

/**
 * User.
 *
 * @ORM\Table(name="user")
 * @ORM\Entity()
 */
class User extends BaseUser
{

    (...)

    /**
     * @var Frais[]
     *
     * @ORM\OneToMany(targetEntity="App\Entity\Frais", mappedBy="user",indexBy="label")
     */
    private $frais;

在这里,我的表单字段:

->add(
            'frais',
            CollectionType::class,
            [
                'entry_type'   => FraisType::class,
                'allow_add'    => true,
                'allow_delete' => true,
                'by_reference' => false,
                'prototype'    => true
            ]
        )

即使我在FraisType中设置了以下功能:

 public function getBlockPrefix()
{
    return 'frais';
}

/**
 * {@inheritDoc}
 */
public function getName()
{
    return 'frais';
}

/**
 * {@inheritDoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        [
            'data_class' => Frais::class
        ]
    );
}

我发现的唯一解决方案是将CollectionType更改为EntityType。您知道有什么解决方案可以避免我更改此设置吗?

1 个答案:

答案 0 :(得分:0)

所以。我选择使用“数据”表单字段选项。 这并不是我的问题的真正答案,但确实有效

            ->add(
                'frais',
                CollectionType::class,
                [
                    'entry_type'   => FraisType::class,
                    'allow_add'    => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                    'prototype'    => true,
                    'data' => $this->em->getRepository(FraisDivers::class)->findBy(['user' => $this->user])
                ]
            )

这样可以防止通过关系链接获取Frais实体,并防止indexOf选项混乱。