映射实体和另一个实体彼此不一致

The mappings Entity and another Entity are inconsistent with each other

我的实体之间的关系有问题。

我的第一个实体用户#tickets:

class User 
{
    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Ticket", mappedBy="user")
     */
    private $tickets;

}

我的第二个实体Tickets#responsible:

class Ticket
{

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="tickets")
     */
    private $user;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="tickets")
     */
    private $responsible;
}

它给我一个映射错误:

The mappings App\Entity\Ticket#responsible and App\Entity\User#tickets are inconsistent with each other.

If association App\Entity\Ticket#responsible is many-to-many, then the inversed side App\Entity\User#tickets has to be many-to-many as well.

但是字段都是ManyToMany?

如果关系的一侧是ManyToOne,那么关系的另一侧必须是OneToMany

您有两个从 Ticket 到 User 的关系。一个是 ManyToOne,您的映射似乎在说:User 可能有许多 Ticket

Ticket::$user这边好像没问题:

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="tickets")
 */
private $user; 

但是反比关系错了就是错了。它是 ManyToMany,但它指向 ManyToOne。修复它,使其成为 OneToMany,它应该可以工作。

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Ticket", mappedBy="user")
 */
private $tickets;

你也有 ManyToManyTicketUser,但你试图使用相同的 属性 来反转关系,这不会感觉。

如果要映射Ticket::$responsible的反面,需要在User上再添加一个属性。

例如:

// User entity
/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Ticket", mappedBy="responsible")
 */
private $tickets_responsibility;
// Ticket entity
/**
 * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="tickets_responsibility")
 */
private $responsible;