如何使用Symfony EasyAdmin 4中的关联领域4

发布于 2025-01-20 03:23:44 字数 515 浏览 4 评论 0原文

当我使用时:

public function configureFields(string $pageName): iterable
    {
      return [
            AssociationField::new('XYZ')
        ];
    }`

我收到错误 “App\Entity\XYZ 类的对象无法转换为字符串”

当我将 ->autocomplete() 添加到 AssociationField::new('XYZ'),然后它可以工作,但在保存时显示错误 “预期参数类型为“?string”,“App\Entity\XYZ”在属性路径中给出” XYZ"。

什么使用此字段与多对一关系的正确方法是什么? ://symfony.com/doc/current/EasyAdminBundle/fields/AssociationField.html 根本没有帮助。

When I use:

public function configureFields(string $pageName): iterable
    {
      return [
            AssociationField::new('XYZ')
        ];
    }`

I get error "Object of class App\Entity\XYZ could not be converted to string"

When I add ->autocomplete() to the AssociationField::new('XYZ'), then it works, but on save it displays error "Expected argument of type "?string", "App\Entity\XYZ" given at property path "XYZ".

Whats the CORRECT way to use this field with many to one relation? Symfony Easy Admin documentation https://symfony.com/doc/current/EasyAdminBundle/fields/AssociationField.html doesn't help at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2025-01-27 03:23:44

您的实体 App\Entity\XYZ 将转换为关联字段中的字符串(这是标准的 symfony 实体类型)。否则无法在实体选择中设置标签。

它将尝试使用 __toString 方法对其进行转换,因此您需要将其添加到您的实体中。

例如:

/**
 * @ORM\Entity(repositoryClass=XyzRepository::class)
 */
class Xyz
{
  public function __toString(){
    return $this->name; //or anything else
  }

EasyAdmin 应该能够猜测实体类,因此您不必像简单的 EntityType 那样指定它。

Your entity App\Entity\XYZ will be converted to a string in your association field (which is a standard symfony entity type). Otherwise there is no way to set a label in your entity select.

It will try to convert it using the __toString method, so you need to add it in your entity.

For example:

/**
 * @ORM\Entity(repositoryClass=XyzRepository::class)
 */
class Xyz
{
  public function __toString(){
    return $this->name; //or anything else
  }

EasyAdmin should be able to guess the entity class, so you don't have to specify it like you would for a simple EntityType.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文