首页 > 解决方案 > EasyAdminBundle 3:带有实体的集合字段

问题描述

我有两节课:

class Product {
     /**
     * @ORM\OneToMany(targetEntity=Keyword::class, mappedBy="product")
     */
    private $keywords;
}

class Keyword {
     /**
     * @ORM\Column(type="string", length=255)
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity=Product::class, inversedBy="keywords")
     */
    private $product;
}

在我定义的产品 CRUD 控制器中:

public function configureFields(string $pageName): iterable
{
     yield CollectionField::new('keywords');
}

它正确显示了所有关键字,但是当我尝试保存它时出现错误:

属性路径“keywords”中给出的“App\Entity\Keyword”、“string”类型的预期参数。

是否有可能将这种联系显示为集合?以这种方式管理关键字会容易得多。

标签: phpsymfonysymfony5easyadmin

解决方案


你需要使用

yield AssociationField::new('keywords');

代替

yield CollectionField::new('keywords');

推荐阅读