在 EasyAdmin 3 中使用自动完成实现 CollectionField

Implement CollectionField with Autocomplete in EasyAdmin 3

在 EasyAdmin 3(我目前使用 3.1.2)中,我正在尝试实现一个 CollectionField,它将允许引用其他实体的集合。

想法是创建一个 GameTop,它引用其他游戏。

Crud 的实体是 GameTop,我将 CollectionField 添加为:

$games = CollectionField::new('games')
            ->allowAdd()
            ->allowDelete()
            ->setEntryType(GameTopEntryType::class)
            ->showEntryLabel(false)
        ; 

GameTopEntryType class 如下。我创建它是为了能够将游戏作为 EntityField 进行引用,但我不知道这是否是正确的方法。

class GameTopEntryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('game', EntityType::class, [
                'class' => Game::class,
                'label' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Game::class,
        ]);
    }
}

表单看起来没问题,但是在将游戏添加到 GameTop 时,会触发以下错误:

Neither the property "game" nor one of the methods "getGame()", "game()", "isGame()", "hasGame()", "__get()" exist and have public access in class "App\Entity\Game".

我一定没有正确实现 CollectionField,有谁知道我做错了什么(或在集合中使用 EasyAdmin 的自动完成字段)?

谢谢

有了EasyAdmin 3.4.1版本,我们可以简单的做到:

$games = AssociationField::new('games')
            ->setCrudController(GameCrudController::class)->autocomplete();

这将允许添加多个条目。重要的是要注意此解决方案不会对元素进行排序。