简体   繁体   中英

Symfony2 and Doctrine multiple ManyToMany relation

I have a model with three main elements: Questions, Answers and Games. Each of them whould have Tags associated, but those Tags needs to be shared. I mean, the same tags should be used for the three kind of elements: the "Science" tag should be applied to either a Question, an Answer or a Game, or the three of them.

How should I do this? Do I add a ManyToMany field in the Tags entity for each kind of element, or is there any other way to relate them? Any idea?

I would choose option 1) add ManyToMany relationship on each entities.

class Question
{
    // ... 

    /**
     * @ORM\ManyToMany(targetEntity="Tag")
     *
     **/
    private $tags;
}

class Answer
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Tag")
     *
     **/
    private $tags;
}

class Game
{
    // ...

    /**
     * @ORM\ManyToMany(targetEntity="Tag")
     *
     **/
    private $tags;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM