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);
0
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
.