【问题标题】:Easyadmin 3: field is not a Doctrine association, so it cannot be used as an association fieldEasyadmin 3:字段不是Doctrine关联,所以不能作为关联字段
【发布时间】:2021-02-10 08:07:39
【问题描述】:

所以情况是这样的: 我有一个实体 ProductWhisky,它是 ProductAbstract 的子类,我在其中引用 Product 实体(一对一)。 Product 与 ProductProducer 具有多对一关联。我正在使用 Easyadmin 3 来编辑这个 ProductWhisky。

所以这是我的 ProductWhiskyCrudController 的一部分:

public function configureFields(string $pageName): iterable
{
    // more fields
    yield AssociationField::new('productProducer', 'Producent');
    // even more fields
}

这是我的 ProductWhiskyEntity:

class ProductWhisky extends ProductAbstract
{
    use ProductTrait;
    //props and other methods specific to ProductWhisky
}

产品特性

trait ProductTrait
{
    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Product", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $product;

    public function getProduct(): ?Product
    {
        return $this->product;
    }

    public function setProduct(Product $product): self
    {
        $this->product = $product;

        return $this;
    }

    /**
     * @return ProductProducer|null
     */
    public function getProductProducer(): ?ProductProducer
    {
        if ($this->getProduct()) {
            return $this->getProduct()->getProducer();
        }
        return null;
    }

产品实体:

class Product
{
    // more props
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\ProductProducer", inversedBy="products", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $producer;
    
    // more props and methods

    public function getProducer(): ?ProductProducer
    {
        return $this->producer;
    }

    public function setProducer(?ProductProducer $producer): self
    {
        $this->producer = $producer;

        return $this;
    }

所以,当我调用 ProductWhiskyCrudController 时,我总是收到消息 The "productProducer" field is not a Doctrine association, so it cannot be used as an association field.

在 EasyAdmin 2 中,我这样做了:

      form:
        fields:
          - property: 'productProducer'
            label: 'Producent'
            type: entity
            type_options: {class: 'App\Entity\ProductProducer', required: true}

知道如何解决这个问题吗?

一切顺利 蒂姆

【问题讨论】:

    标签: symfony doctrine easyadmin


    【解决方案1】:

    你应该改变你的

    yield AssociationField::new('productProducer', 'Producent');
    

    yield AssociationField::new('producer', 'Producent');
    

    因为您的表单必须引用字段,而您将其引用到实体。

    【讨论】:

    • 同样的问题,请问控制器应该使用哪个实体?
    • 我重构了 ProductWhisky 实体,所以它不再包含 Product 实体,更容易获取所有数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多