Easyadmin 3: field is not a Doctrine association, so it cannot be used as an association field

Viewed 4940

so this is the situation: I have an entity ProductWhisky, which is a subclass of ProductAbstract, in which I refer to the Product entity (One-to-One). Product has many-to-one association to ProductProducer. I'm using Easyadmin 3 to edit this ProductWhisky.

So this is a part of my ProductWhiskyCrudController:

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

This is my ProductWhiskyEntity:

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

ProductTrait

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;
    }

Product entity:

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;
    }

So, when I call the ProductWhiskyCrudController, I always get the message The "productProducer" field is not a Doctrine association, so it cannot be used as an association field.

In EasyAdmin 2, I did this with this:

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

Any idea how I could fix this?

All the best Tim

1 Answers

You should change your

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

with

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

because your form has to refer to a Field, while you are referring it to an entity.

Related