johannes
johannes

Reputation: 31

The mappings Entity and another Entity are inconsistent with each other

I have a problem with my relations between my entities.

My first entity User#tickets:

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

}

My second entity Tickets#responsible:

class Ticket
{

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

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

It gives me a mapping error:

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.

But the fields are both ManyToMany?

Upvotes: 1

Views: 1397

Answers (1)

yivi
yivi

Reputation: 47308

If one side of the relationship is ManyToOne, then the other side of the relationship has to be OneToMany.

You have two relationships going from Ticket to User. One is a ManyToOne, where your mapping seems to say: a User may have many Tickets.

This side of the Ticket::$user seems to be fine:

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

But the inverse relationship is wrong is wrong. It's a ManyToMany but it's pointing to the ManyToOne. Fix it so it's a OneToMany, and it should work.

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

You also have ManyToMany going from Ticket to User, but you are trying to use the same property to inverse the relationship, which doesn't make sense.

If you want to map the inverse side of Ticket::$responsible, you need to add another property to User.

E.g.:

// 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;

Upvotes: 2

Related Questions