如何在 Symfony 4 (ManyToMany) 中反转我的专栏?

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

这是我的实体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();
  }


}

还有我的实体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;
    }
}

我的数据表 "Fields" 中的输出是这样的:

我的数据表 "productgroup" 中的输出是这样的:

我喜欢现在在我的产品组中的反向我还看到了连接到产品组的字段。

我试图在行中添加一个 inverseredBy

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

但是我没有得到我想要的结果。

您正在寻找多对多关系,但 Bidirectional

你的情况应该是

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

并且对于 Productgroup 实体

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

请注意,inversedBy 指的是实体 属性,因此它应该是 fields,而不是 Fields