Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

php - One entity having 2 ManyToOne relations to the same other entity

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would suggest to have ManyToMany relation between Wedding and Character entities if there is no such specific reason or differentiation between character 1 and character 2

class Character
{
    // ...
    
    /**
    * @ORMManyToMany(targetEntity=Wedding::class, mappedBy="characters")
    */
    private $weddings;
}

class Wedding
{
    // ...
    
    /**
     * @ORMManyToMany(targetEntity=Character::class, inversedBy="weddings")
     */
    private $characters;
}

This way you can scale it in future if you have more characters to be assigned to a wedding

If there are any strict actions that need to be performed specific to character 1 or character 2 depends on your needs, then you can use OneToMany and ManyToOne as

class Character
{
    // ...

    /**
     * @ORMOneToMany(targetEntity=Wedding::class, mappedBy="characterA")
     */
    private $weddingsA;

    /**
     * @ORMOneToMany(targetEntity=Wedding::class, mappedBy="characterB")
     */
    private $weddingsB;
}

class Wedding
{
    // ...

    /**
     * @ORMManyToOne(targetEntity=Character::class, inversedBy="weddingsA")
     */
    private $characterA;
    
    /**
     * @ORMManyToOne(targetEntity=Character::class, inversedBy="weddingsB")
     */
    private $characterB;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...