在 EasyAdmin3 中配置 AssociationField 默认属性
我有一个问题 - 是否可以配置 AssociationField 以使用特定属性。即:
我有一个与用户具有多对一关系的订阅实体,用户有一个 __toString() 方法,该方法返回用户名,并且它在整个应用程序中使用,所以我无法更改它。在“创建订阅”表单中,我有 AssociationField::new('user'),我可以在其中通过用户的名字找到用户。
但这很不方便,因为当我需要创建订阅时,会弹出许多同名的用户。相反,我希望能够通过 ID 或电子邮件搜索用户。
有没有办法覆盖默认行为?
I have a question - is here a possibility to configure AssociationField to work with specific property. i.e:
I have a Subscription Entity with a many-to-one relation to User, User has a __toString() method, that returns username, and it is used across the application, so I can't change it. In the 'create Subscription' form, I have AssociationField::new('user'), where I'm able to find User by his name.
But this is inconvenient, since, when I need to create a Subscription, many users with same names pop up. Instead, I want to be able to search Users by ID, or email.
Is there a way to override default behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
AssociationField
是使用 Symfony EntityType 创建的。如果您查看此字段使用的表单类型。这意味着您可以使用其中的每个选项。 在此处查看更多信息。
就您而言,通过定义另一个属性或回调来修改标签非常容易。
然后就可以使用
->setFormTypeOption()
来修改实体类型选项。所以如果你想使用回调函数来定义自定义标签:
或者使用 php 7.4 箭头函数:
你还可以将 email 属性定义为标签:
Your
AssociationField
is made using Symfony EntityType. If you look into the form type used by this field.It means that you can use every options from it. See more here.
In your case, it's quite easy modifying your label by either defining another property or a callback.
Then you can use the
->setFormTypeOption()
to modify the entity type option.So if you want to use a callback function to define a custom label:
Or using php 7.4 arrow function:
You can also define the email property as label: