0

In the entity products I am relating my products to my productgroup:

/**
 * @ORM\ManyToOne(targetEntity="Productgroup")
 * @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
 */
private $productgroup;

This i working well.

So for example this is the result:

Product      Productgroup

Frog         Animal
Fish         Animal
Pizza        Food

But sometimes my products have multiple productgroups like this:

Frog         Animal
Fish         Animal, Food
Pizza        Food

So I changed my entity to this:

/**
 * @ORM\ManyToMany(targetEntity="Productgroup")
 * @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
 */
private $productgroup;

  public function getProductgroup(): ?Productgroup
    {
      return $this->productgroup;
    }

    public function setProductgroup(?Productgroup $productgroup): self
    {
      $this->productgroup = $productgroup;

      return $this;
    }

Now I get the error message:

Return value of App\Entity\Products::getProductgroup() must be an instance of App\Entity\Productgroup or null, instance of Doctrine\ORM\PersistentCollection returned

peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

1

Inner storage for collection of related entities is PersistentCollection in your case.

So, property productgroup is not a ProductGroup entity. It is collection of ProductGroup entities. For simplicity you can consider it like "array of items".

According to said above, your getProductgroup does not return instance of ProductGroup, it returns collection of ProductGroups, that's why this method should be rewritten as:

// no typehint here
// Of course you can use `PersistentCollection` typehint, it is your choice
// main point that returned value is NOT a `ProductGroup` instance
public function getProductgroup()
{
    return $this->productgroup;
}

Second, as property productgroup is not a ProductGroup, your setProductgroup method should be rewritten too:

// I omit typehint as you pass not instance of `Productgroup`, but `collection`
public function setProductgroup($productgroup): self
{
    $this->productgroup = $productgroup;

    return $this;
}

Usually, to add one instance of Productgroup to collection, there should be an adder method:

// here, you can typehint argument, because it is useless 
// to pass anything else except `Productgroup` instance
public function addProductgroup(Productgroup $productgroup): self
{
    $this->productgroup[] = $productgroup;

    return $this;
}

Also, it is a good point to init $this->productgroup as empty collection in constructor:

public function __construct()
{
    $this->productgroup = new ArrayCollection();
}

Some other info about PersistentCollection and ArrayCollection.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Thank you, I tested your suggestion. I do not have an error message anymore. But I do not see in the mySQL database where they are actually connected. – peace_love Jan 03 '19 at 08:36
  • 1
    If it is ManyToMany relation there should be a so called `pivot` table `product_product_group` or similar. Of course if you updated db schema or created a migration using changes in your models. – u_mulder Jan 03 '19 at 08:40