Как я могу перевернуть мою колонку в Symfony 4 (ManyToMany)? - PullRequest
0 голосов
/ 21 января 2019

Это моя сущность 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;
    }
}

Вывод в моих полях с данными, которые можно датировать, таков:

enter image description here

И вывод в моей «товарной группе» с данными:

enter image description here

Мне нравится, что теперь в моей группе продуктов все наоборот, я вижу также поля, которые связаны с группой продуктов.

Я попытался добавить inverseredBy в строку:

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

НоЯ не получаю желаемого результата.

1 Ответ

0 голосов
/ 21 января 2019

Вы ищете отношения «Многие ко многим», хотя Двунаправленный

В вашем случае это должно быть

  /**
   * @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

...