I'm working with Symfony5. I have 2 entities with relationships, Character and Wedding.
Each Character can have many Wedding.
Each Wedding is related to 2 different Character.
/**
* @ORMEntity(repositoryClass=CharacterRepository::class)
*/
class Character
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=50, nullable=true)
*/
private $firstName;
/**
* @ORMColumn(type="string", length=50, nullable=true)
*/
private $lastName;
[...]
And Wedding :
/**
* @ORMEntity(repositoryClass=WeddingRepository::class)
*/
class Wedding
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=4, nullable=true)
*/
private $startYear;
/**
* @ORMColumn(type="string", length=4, nullable=true)
*/
private $endYear;
[...]
}
I tried to set 2 different ManyToOne relations in Wedding entity (persona1 and persona2) but persona1 and persona2 had the same inversedBy="weddings", so it's not working.
I tried 2 ManyToMany relations too, but Doctrine didn't like it :
// Character
/**
* @ORMManyToMany(targetEntity=Wedding::class, mappedBy="persona1")
*/
private $weddings1;
/**
* @ORMManyToMany(targetEntity=Wedding::class, mappedBy="persona2")
*/
private $weddings2;
// Wedding
/**
* @ORMManyToMany(targetEntity=Character::class, inversedBy="weddings1")
*/
private $persona1;
/**
* @ORMManyToMany(targetEntity=Character::class, inversedBy="weddings2")
*/
private $persona2;
The mappings AppEntityWedding#persona2 and AppEntityCharacter#weddings are inconsistent with each other.
What is the good relationship, ManyToOne or ManyToMany, since each Wedding is related to 2 Characters ? How to make it works with Doctrine ?
Thanks for all suggestion !
Ash
question from:
https://stackoverflow.com/questions/65901266/one-entity-having-2-manytoone-relations-to-the-same-other-entity 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…