0

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
pdd
  • 342
  • 1
  • 3
  • 11
  • possible duplicate of [Doctrine2: Best way to handle many-to-many with extra columns in reference table](http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table) – Crozin Feb 19 '11 at 16:41

1 Answers1

0

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.

Cobby
  • 5,273
  • 4
  • 28
  • 41