EntityType作为输入字段? [英] EntityType as an input field?

查看:58
本文介绍了EntityType作为输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有CollectionType字段的表单,该字段包含一个我想根据需要添加和删除的电子邮件字段(这是实体的两个字段之一).

I'm trying to create a form with a CollectionType field containing an email field (which is one of two fields of an entity) which I want to add and remove as I wish.

这是场景:

我有三个实体: User Cluster 以及两者之间的关系,称为 ClusterAdministrator .

I have three entities: User, Cluster and the relationship between both called ClusterAdministrator.

实体 User 具有很多全方位的属性(ID,名称,电子邮件等).实体 Cluster 也具有很多属性,其中之一是 administrators ,它是一个ArrayCollection.实体 ClusterAdministrator 具有3个属性:id,用户和集群.

Entity User has plenty of all-around attributes (id, name, email, etc). Entity Cluster has a lot of attributes too, and one of them is administrators, which is an ArrayCollection. Entity ClusterAdministrator has 3 attributes: id, user and cluster.

我的BD大约有5万名用户.如果我添加一个UserType的CollectionType字段,并且我的UserType是带有 user 作为普通EntityType的表单,则应用将崩溃,因为渲染这样一个巨大的选择框需要30秒钟以上的时间.如果我的UserType是具有 user 作为电子邮件类型的表单,则该表单呈现良好(请参见此处),但是当我单击保存"时,出现错误消息:

My BD has around 50K users. If I add an CollectionType field of a UserType, and my UserType is a form with user as a normal EntityType, app will crash because it takes more than 30seconds to render such a huge select box. If I my UserType is a form with user as an EmailType, the form is well rendered (see here), but when I click save I get the error:

类型为"App \ AppBundle \ Entity \ User"的预期参数,已给出"string"

Expected argument of type "App\AppBundle\Entity\User", "string" given

我如何仅通过显示电子邮件字段来完成这项工作?

How do I make this work just by showing the email field?

对不起,我是Symfony的新手.感谢您的宝贵帮助.

Sorry guys, I'm quite newbie to Symfony. I'll appreciate all of your precious help.

推荐答案

您应使用 Symfony 中提供了Data Transformer .转换器所做的就是将字符串(用户名或电子邮件)转换为相关的User对象.哪个应该可以解决您的问题.

You should use Data Transformer available in Symfony. All that transformer does is, transform the string(User name or email) to relevant User object. Which should solve your problem.

为了获得更好的用户体验,您可以在jQuery的帮助下将文本框转换为 autocomplete .我早就用了.我使用了 PUGXAutoCompleteBundle 生成了自动填充字段,并创建了一个自定义的 Transformer 来填充父实体的对象.

For a better user experience, you can convert the text box to an autocomplete with the help of jQuery. I used it long back. I used PUGXAutoCompleteBundle to generate an autocomplete field and created a custom Transformer to feed the object to parent entity.

如果需要的话,我可以在明天晚些时候分享一些代码段.

I can share some code snippet later by tomorrow if required.

希望这会有所帮助!

这篇关于EntityType作为输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆