-1

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.

peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

1

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

Majesty
  • 2,097
  • 5
  • 24
  • 55
  • I just tested it and get an error message: `The annotation "@ManyToMany" in property App\Entity\Productgroup::$fields was never imported. Did you maybe forget to add a "use" statement for this annotation?` – peace_love Jan 21 '19 at 12:58
  • Add namespace, use `@ORM\ManyToMany` instead of `@ManyToMany` and don't forget to generate new database table `productgroups_fields` – Majesty Jan 21 '19 at 13:01
  • In Productgroup, do I need to add also addFields and getFields and build ArrayCollection? – peace_love Jan 21 '19 at 13:37