Assuming I have three entities: Article, Tag & User. Articles and Tags are many-to-many related.
What I'd like to do is add User information to the many-to-many relationship - so I can tell which user added which tag to an article. Basically, the result in SQL should be a 开发者_运维知识库third column 'user_id' in article_tags (the many-to-many relationship table containing article_id and tag_id keys) that I can query for.
Right now I'm only aware of one solution: Creating a fourth entity 'ArticleTags' that has three relationship fields article, tag and user. Showing all tags in an article then becomes...
<?php
foreach($article->getArticleTags() AS $articleTag) {
echo $articleTag->getTag()->getName()
}
...which is much less straight-forward than $article->getTags() already, plus it requires all kinds of repository trickery for querying, sorting and so forth.
Is there a more ideal solution for this kind of thing? I'm using Doctrine 2.1.0-DEV right now. Thanks.
IMO, I think the ArticleTags entity is a good approach.
Alternatively, if the User entity has associations with Articles and Tags, you should be able to map out from the User entity which tags belong to which post for that specific user.
精彩评论