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.
/**
* @ORM\Entity(repositoryClass=CharacterRepository::class)
*/
class Character
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $lastName;
[...]
And Wedding :
/**
* @ORM\Entity(repositoryClass=WeddingRepository::class)
*/
class Wedding
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=4, nullable=true)
*/
private $startYear;
/**
* @ORM\Column(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
/**
* @ORM\ManyToMany(targetEntity=Wedding::class, mappedBy="persona1")
*/
private $weddings1;
/**
* @ORM\ManyToMany(targetEntity=Wedding::class, mappedBy="persona2")
*/
private $weddings2;
// Wedding
/**
* @ORM\ManyToMany(targetEntity=Character::class, inversedBy="weddings1")
*/
private $persona1;
/**
* @ORM\ManyToMany(targetEntity=Character::class, inversedBy="weddings2")
*/
private $persona2;
The mappings App\Entity\Wedding#persona2 and App\Entity\Character#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