Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a crazy question, consider I'm developing a blog website, There is two model named Post and Comment, now I wanna add a method to fetch all comments from database, which model should I choose to add this method to? Post or Comment.

Approach 1:

$post= new Post();
$post->setID($id);
$r = $post->loadAllComments();

Approach 2:

$r = Comment::loadAllComments($post_id);
share|improve this question

1 Answer 1

up vote 0 down vote accepted

I would use approach 2, putting it on the Comment model. I'd name the method something like loadAllCommentsForPost though, to clarify that it doesn't just load every single comment from the database.

Though approach 1 wouldn't be too bad, consider that you might have a lot more models relating to Post later on. You wouldn't want your Post model cluttered with methods like loadAllLikes and loadAllRetweets. It would be much cleaner to split those up to their respective models - Likes and Retweets.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.