Symfony2 Doctrine - 两个相同实体之间的两种不同关系?

Symfony2 Doctrine - two different relations between two thesame entities?

我使用 Symfony 2 和 Doctrine ORM

我想在我的项目中创建一个模块,每个模块都有帖子和评论。

我有实体 Profile(存储用户)和 Comments(显然存储评论)

我需要在相同的实体之间创建两种不同的关系。

首先需要在Comments实体中存储评论的作者(1对1的关系)。 但其次,对于每个评论,都希望添加每个用户都可以使用的选项 "like" 和 "dislike"。 我想我需要 ProfileComment 实体之间的多对多关系,这些实体将存储有关 like/dislike 操作的信息。

我从未创建过 2 个实体,它们之间有两种不同的关系。你有这方面的经验吗?或者也许我以错误的方式处理这个问题,应该采取与我认为不同的方式。

请提供任何帮助或线索。

你可以在协会的学说文档中看到一个非常相似的例子:

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#association-example-entities

将其放入您的用例中非常容易。 (注意:未经测试的复制粘贴代码来袭!)

Profile中你定义:

/**
 * Bidirectional - Many users have Many liked comments (OWNING SIDE)
 *
 * @ManyToMany(targetEntity="Comment", inversedBy="profileLikes")
 * @JoinTable(name="profile_liked_comments")
 */
private $commentsLiked;

/**
 * Bidirectional - Many users have Many liked comments (OWNING SIDE)
 *
 * @ManyToMany(targetEntity="Comment", inversedBy="profileDislikes")
 * @JoinTable(name="profile_disliked_comments")
 */
private $commentsDisliked;

/**
 * Bidirectional - One-To-Many (INVERSE SIDE)
 *
 * @OneToMany(targetEntity="Comment", mappedBy="author")
 */
private $commentsAuthored;

并在 Comment 中定义:

/**
 * Bidirectional - Many comments are liked by many users (INVERSE SIDE)
 *
 * @ManyToMany(targetEntity="Profile", mappedBy="commentsLiked")
 */
private $profileLikes;

/**
 * Bidirectional - Many comments are disliked by many users (INVERSE SIDE)
 *
 * @ManyToMany(targetEntity="Profile", mappedBy="commentsDisliked")
 */
private $profileDislikes;

/**
 * Bidirectional - Many Comments are authored by one user (OWNING SIDE)
 *
 * @ManyToOne(targetEntity="Profile", inversedBy="commentsAuthored")
 */
 private $author;

请注意,我将 ProfileComment 的关系从 OneToOne 更改为 OneToMany,因为我假设用户应该能够写多个评论。