Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6668279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:01:28+00:00 2026-05-26T03:01:28+00:00

I’m posting a ghost here, that daunts me for some years. It’s a question

  • 0

I’m posting a ghost here, that daunts me for some years. It’s a question about how to build the right model, the right objects.

Let me explain. Suppose I have a class Article. An Article has title, rating, body copy and comments.

The class Comment has author, timestamp, text.

An Article can have 0 or more comments. So far, so good. No problem with this concept. But…

  • When showing the Article, I show everything. Article properties, including its comments.
  • When showing a list of articles, I show only article name and a few of body copy.

Here is where I got confused because I don’t need to load the comments information, and this can make a significant performance difference when I have lots of articles and tons of comments.

Should I build two models? One for Article and one for ArticlesInList? Should I delegate the load of comments to a lazy mode (is this possible), retrieving them only when necessary?

What is the right way to face and solve this?

Thx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:01:29+00:00Added an answer on May 26, 2026 at 3:01 am

    There are a lot of trade-offs to be made when trying to model your business objects.

    Given your example, I can think of a few approaches, that mostly revolve around lazy-loading comments. Here’s how I’d do it, if I were reasonably sure that things weren’t going to get more complex:

    First, you create entities for Article and Comment that simply represent the data in each table in your database. Write setters and getters. Implement a loadComments() method on Article.

    Implement one or more Collection classes, such as ArticleCollection. You might have a service class that fetches articles that match some criteria. ArticleService::fetchArticles() would return articles without comments loaded. Then implement a loadComments() method of ArticleCollection that loads all the comments for all the articles in the collection. At first, this can just iterate over the articles calling loadComments — but you can replace it later with a single-query implementation.

    Here’s the beginning of an Article and ArticleCollection. If you implement a CommentCollection class, you could use that inside Article to hold the comments, etc.

    <?php
    /**
     * Extends a base model class that provides database-related methods -- not ideal, 
     * but trying to stay focused here.
     */
    class Article extends Model {
        private $_data;
        private $_fields = array('id','title','body','author');
    
        /** 
         * Constructor can take an array of values to initialize.
         */
        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']; }
        // more getters, and setters, here.
    
        public function loadComments(){
            $result = $this->query('SELECT * FROM Comment WHERE article_id = ' . $this->getId());
            $this->_comments = array();
    
            foreach($result as $c){
                //instantiate a new comment (imagine Comment's constructor is very similar to Article's
                $this->_comments[] = new Comment($c);
            } 
        }
    }
    
    class ArticleCollection extends Model {
        /**
         * An array of Articles, indexed by article_id
         */
        private $_articles = array();
    
        /**
         * Naive implementation.  A better one would grab all article IDs from $this->_articles, and
         * do a single query for comments WHERE article_id IN ($ids), then attach them to the 
         * right articles.
         */
        public function loadComments(){
            foreach($this->_articles as $a){
                $a->loadComments();
            }
        }
    
        /**
         * Add article to collection
         */
         public function addArticle(Article $article){
             if (empty($article->id)) throw new \Exception('Can\'t add non-persisted articles to articlecollection!');
             $this->_articles[$article->id] = $article;
         }
    }
    

    The above is pretty basic — you can apply other design patterns to factor out your database-access so it’s not so tightly coupled, for instance. But I’m just trying to describe a strategy for lazy-loading your comments here in a sane way.

    Some final advice: Don’t fall into the trap that many framework do and think there is some divine correlation between tables in your database and models. Models are just objects. They can do different kinds of things (represent a simple thing like a comment, or a user), or represent things like a service that operates on those kinds of simple things, or they can be things like groups (collections) of those individual things.

    One fun exercise is to just write up classes, and fill them up with dummy-data. Do your best to completely forget that a database will be involved. Craft objects that support the use-cases you need. Then, once you’ve got that done, figure out how to save and load the data to/from the DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.