Doctrine orm 无法使条令2多对多关系发挥作用

Doctrine orm 无法使条令2多对多关系发挥作用,doctrine-orm,Doctrine Orm,我有两个实体-一个用户和一个标签 这是我的用户: <?php namespace Project\Model; /** * @Entity * @Table(name="users") * @InheritanceType("JOINED") * @DiscriminatorColumn(name="discr", type="string") * @DiscriminatorMap({"user" = "User", "client" = "Client", "staff" =

我有两个实体-一个用户和一个标签

这是我的用户:

<?php
namespace Project\Model;
/**
 * @Entity
 * @Table(name="users")
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"user" = "User", "client" = "Client", "staff" = "Staff"})
 **/
class User implements \JsonSerializable {
  /** @Id @Column(type="integer") @GeneratedValue **/
  protected $id;

  /** @Column(type="string", name="first_name") **/
  protected $firstName;

  /**
   * @ManyToMany(targetEntity="Project\Model\Tag", inversedBy="users")
   * @JoinTable(name="user_tags")
   **/
  protected $tags;

  /**
   * Construct a new user.
   */
  public function __construct() {
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
  }

  // Getters

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

  public function getFirstName() {
    return $this->firstName;
  }

  public function getTags() {
    return $this->tags;
  }

  // Setters

  public function setFirstName($firstName) {
    $this->firstName = $firstName;
  }

  /**
   * Add a tag to a user.
   * @param Tag
   */
  public function addTag(Tag $tag) {
    $tag->addUser($this);
    $this->tags[] = $tag;
  }
}

我想问题可能来自你的许多亲戚。你用

@ManyToMany(targetEntity="Tag", inversedBy="users")
然而,您应该有如下内容(当然,假设您使用的是Symfony 2):

此外,您使用了一个
inversedBy
,但没有使用
mappedBy
,因此映射无效


最后一个更为详细,但在标记类中命名属性“tag”并不是最干净的。也许把它改成“名字”

在标记类中,可以使用以下方法引用用户类:

/** 
 *  @ManyToMany(targetEntity="Bix\Model\User", mappedBy="tags")
 */
“Bix”应该是“项目”吗?除非这是你问题中的一个输入错误,否则会引起问题

一方应“拥有”关联,并负责在添加时设置反向关联

<?php

// Assuming that the User is the "owning side".

class User {

    // Mappings as you have them, minus the "Bix" namespace thing.

    public function getTags()
    {
        return $this->tags;
    }

    public function addTag(Tag $tag)
    {
        $tag->addUser($this);
        $this->tags->add($tag);
    }

    public function removeTag(Tag $tag)
    {
        $tag->removeUser($this);
        $this->tags->removeElement($tag);
    }
}

class Tag {

    // Mappings as you have them, minus the "Bix" namespace thing.

    public function getUsers()
    {
        return $this->users;
    }

    public function addUser(User $user)
    {
        $this->users->add($user);
    }

    public function removeUser(User $user)
    {
        $this->users->removeElement($user);
    }
}

我已经更新了我的问题,将我的名称空间包括在内。而且我没有使用Symfony。我应该在哪里设置mappedBy?要么删除“InverdBy”映射,并且实体之间存在单向关系,要么在标记实体中添加一个带有注释的$users属性:
@manytomy(targetEntity=“Project\Model\User”,inversedBy=“tags”)
,然后它变成双向的。I使用第二个选项,然而,它仍然不起作用。此外,根据条令文件,标签实体注释应为mappedBy,而不是inversedBy。我试过了,但也没用。
@ManyToMany(targetEntity="AppBundle\Entity\Tag")
/** 
 *  @ManyToMany(targetEntity="Bix\Model\User", mappedBy="tags")
 */
<?php

// Assuming that the User is the "owning side".

class User {

    // Mappings as you have them, minus the "Bix" namespace thing.

    public function getTags()
    {
        return $this->tags;
    }

    public function addTag(Tag $tag)
    {
        $tag->addUser($this);
        $this->tags->add($tag);
    }

    public function removeTag(Tag $tag)
    {
        $tag->removeUser($this);
        $this->tags->removeElement($tag);
    }
}

class Tag {

    // Mappings as you have them, minus the "Bix" namespace thing.

    public function getUsers()
    {
        return $this->users;
    }

    public function addUser(User $user)
    {
        $this->users->add($user);
    }

    public function removeUser(User $user)
    {
        $this->users->removeElement($user);
    }
}