When you try to model business objects, you need a lot of trade-offs.
Given your example, I can come up with several approaches that mostly revolve around lazy comments. Here is how I would do it if I was sure that things were not going to get complicated:
First, you create entities for the article and commentary that simply represent the data in each table in your database. Write setters and getters. Implement the loadComments () method for the article.
Deploy one or more Collection classes, such as ArticleCollection. You may have a class of service that selects articles that meet certain criteria. ArticleService :: fetchArticles () will return articles without uploaded comments. Then we implement the method loadComments () ArticleCollection, which loads all comments for all articles in the collection. At first, this can simply iterate over the articles calling loadComments, but you can later replace them with a single request implementation.
This is where the article and ArticleCollection begins. If you implement the CommentCollection class, you can use this inside the article to store comments, etc.
<?php class Article extends Model { private $_data; private $_fields = array('id','title','body','author'); public function __construct($data=null){ if (is_array($data)){ foreach($this->_fields as $field){ $this->_data[$field] = $data[$field]; } } } public function getId(){ return $this->_data['id']; }
The above is quite simple - you can apply other design patterns to share access to the database so that it is not so closely related, for example. But I'm just trying to describe a strategy for lazily loading your comments here in a reasonable way.
Some final tips: don't fall into the trap that many frameworks adhere to, and believe that there is some divine correlation between the tables in your database and the models. Models are just objects. They can do different things (represent a simple thing, such as a comment or user), or represent things like a service that works with these simple things, or they can be things like groups (collections) of these individual things .
One interesting exercise is to simply write classes and populate them with dummy data. Do your best to completely forget that the database will be involved. Create the objects you need. Then, as soon as you do this, figure out how to save and load data to / from the database.