Search code examples
symfonyjoinmany-to-manyentityinverse

How can I inverse my column in Symfony 4 (ManyToMany)?


This is my entity Fields:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Entity(repositoryClass="App\Repository\FieldsRepository")
*/
class Fields
{
  /**
  * @ORM\Id()
  * @ORM\GeneratedValue()
  * @ORM\Column(type="integer",unique=true)
  */
  private $id;


    /**
    * @ORM\Column(type="string", length=255)
    */

    private $name;

  /**
  * @ORM\Column(type="string", length=10, unique=true)
  */

  private $unique_id;


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



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

  //Getters & Setters

  public function getId()
  {
    return $this->id;
  }



  public function getUniqueId(): ?string
  {
    return $this->unique_id;
  }


  public function setUniqueId(string $unique_id): self
  {
    $this->unique_id = $unique_id;

    return $this;
  }



  public function getName()
  {
    return $this->name;
  }

  public function setName($name)
  {
    $this->name = $name;
  }

  public function getType(): ?Type
  {
    return $this->type;
  }

  public function setType(?Type $type): self
  {
    $this->type = $type;

    return $this;
  }

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

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

    return $this;
  }

  public function addProductgroup(Productgroup $productgroup): self
  {
    $this->productgroup[] = $productgroup;

    return $this;
  }

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


}

And my entity productgroup:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductgroupRepository")
 */
class Productgroup
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=10)
     */
    private $unique_id;



    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUniqueId(): ?string
    {
        return $this->unique_id;
    }

    public function setUniqueId(string $unique_id): self
    {
        $this->unique_id = $unique_id;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

The output in my datatable "Fields" is this:

enter image description here

And the output in my datatable "productgroup" is this:

enter image description here

I Like that now in inverse in my productgroup I see also the fields that are connected to the productgroup.

I tried to add an inverseredBy to the row:

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

But I do not get the result I wish.


Solution

  • You are looking for Many To Many relationship, though Bidirectional

    In your case, it should be

      /**
       * @ManyToMany(targetEntity="Productgroup", inversedBy="fields")
       * @JoinTable(name="productgroups_fields")
      */
      private $productgroup;
    

    And for Productgroup entity

    /**
     * Many Groups have Many fields.
     * @ManyToMany(targetEntity="Fields", mappedBy="productgroup")
     */
    private $fields;
    

    Please, note that inversedBy refers to entity property therefore it should be fields, but not Fields