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

问题描述

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

想法是创建一个引用其他游戏的Gametop。

Crud的实体是Gametop,我用以下方式添加CollectionField:

    Future<CommonResponse> postRecharge(
    String telId,String typeId,String money,String phone) async {
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

  String token = sharedPreferences.getString("apiToken");

  Map data = {
    'telecommunication_id': telId,'telecommunication_type_id': typeId,'money_sent': money,'receiver_mobile': phone,};

  Map<String,String> header = {"Authorization": '$token'};

  var jsonResponse;
  CommonResponse commonResponse;
  var response = await http.post(recharges,headers: header,body: data);

  print(response.statusCode);
  if (response.statusCode == 200) {
    jsonResponse = json.decode(response.body);
    if (jsonResponse != null) {
      commonResponse = new CommonResponse.fromJson(jsonResponse);

      print(commonResponse);
      return commonResponse;
    } else {
      return null;
    }
  } else {
    jsonResponse = json.decode(response.body.toString());
    print(jsonResponse);
    commonResponse = new CommonResponse.fromJson(jsonResponse);

    return commonResponse;
  }
}

GametopEntryType类如下。我创建此程序是为了能够将Game引用为EntityField,但我不知道这是否是正确的方法

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

该表单看起来不错,但是将游戏添加到Gametop时,会触发以下错误

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,]);
    }
}

我一定不能正确实现CollectionField,有人知道我做错了吗(或者在集合中使用EasyAdmin的自动完成字段)?

谢谢

解决方法

使用 EasyAdmin 3.4.1 版本,我们可以简单地做到:

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

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