Many Comments For Multiple Models

Hi, community!

I believe my question is trivial, because there are comments mostly in every application.

I’ve got User, Project and Article models. Every User could leave a comment for Project, Article or another User

What is the right approach: to have just one model Comment and have type attribute for distinguishing them. Or have different models for different comments targets?

If the properties (other than type) and behaviors are the same, make one class (model). If they are similar, make a base class for Comment and extend it with specific classes (e.g., UserComment, ProjectComment, ArticleComment) for the exceptions.

The trick is in the database. You can either build associative tables to relate a single comments table with either users, projects or articles, or you could build separate tables for user-, project- and article comments, each with a foreign key constraint to the users, projects or articles tables, respectively. Associative tables (MANY_MANY) are more scalable, e.g., if you eventually would like a comment to be able to apply to both a user and an article. Separate tables (HAS_MANY) with FKs are easier to implement out of the box with Yii’s active record.

One man’s opinion, of course…

Best,

Joe

use a module

http://www.yiiframework.com/extension/comments-module

http://www.yiiframework.com/extension/comment-module

they both work well

So, if I would stick to comment-module, how I would extend Comments - how to add different properties for different comment types? For example User’s comment will have property rating which will contain, say, 1-5 stars mask.