简体   繁体   English

Symfony2和主义多对多关系

[英]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? 是否为每种元素在标签实体中添加ManyToMany字段,或者是否有其他关联方式? Any idea? 任何想法?

I would choose option 1) add ManyToMany relationship on each entities. 我将选择选项1)在每个实体上添加ManyToMany关系。

class Question
{
    // ... 

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

class Answer
{
    // ...

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

class Game
{
    // ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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