MVC Object Oriented Techniques - How to Minimize Queries and Maintain Flexibility?

I am using the MVC Object Oriented Framework in PHP (Kohana) and have wrinkled a few methods a bit to do something useful. The problem is, I'm not sure how to keep it clean without causing many, many requests on the page.

To illustrate my example, I will imagine that I am designing a stack overflow like a site:

I have a model class for question ( question_model

) and also one for looking up questions ( question_finder_model

).

question_model basically only contains variables for storing question data, arrays of answer objects and some factory methods. Something like:

class question_model {
    public $question_id,$question_title,$question_body,$answers = array();    
}

      

The question finder contains an array of question_model objects as well as an array of question IDs. The array of identifiers is populated by the class search methods and used by other methods. Something like:

class question_finder_model {
    private $question_ids = array();
    public $questions = array() ;   //

    function public find_questions () {
        // executes some SQL to find a list of projects
        // Create a new question_model object for each question and store in $questions
        // for each of these questions store the id in $questions_ids
    }
    function public get_answer_info () {
       // using all the question ids stored in $question_ids:
       // find information about the answers
     }
}

      

So I use this method for all my models, for example my user model will contain an array of question objects.

The problem is that it is difficult to deal with, for example my question contains many answers and each answer may contain many comments, etc. How can I fill all these objects without having multiple requests. I mean, an easy way would be to just loop over my array of question objects and call a function stored in the question class that gets the answer information for that object. But then I would call 10 or 100 requests per page.

I apologize if this is all absurd as the problem is rather difficult to formulate. Any help is appreciated because perhpas my whole picture is messed up.

+2


source to share


3 answers


What you are expressing here is one of the most common preferences with naive ORMappers. Making multiple queries.

You can read the exact description of your problem here: ORMs Done Right: (DBA Gripe # 3: Hidden Expensive Actions)

Basically the problem is that you need multiple requests for something like this:

foreach ($questions in $site) {
 foreach ($question as $questions) {
    foreach ($answer in $question){
       foreach ($comment in $answer) {
         echo "$site->title, $question->title, $answer->title, $comment->title";
       }
    }
 }

      

}



It gets very slow, very fast.

What you need to do is get all the information in the request one by one using the correct connection. And then fill in your objects.

Have a look at the suggested implementation in some article: Class :: ReluctantORM - Mandatory Prefetch

Finally, don't hang up trying to get all the rights. The relational model and impedance mismatch object model are not a problem. Therefore, do not try to fix this problem yourself. After all, Object-Relational Mapping is the Vietnam of Computer Science

+3


source


Actually it doesn't have much to do with PHP than with SQL.

To get a list of answers to a question, including comments, you can get all answers by the question id. Then, collect all response IDs into an array and extract all comments that have a response ID in that array.

These are three requests. You just have to decide which comment belongs to which answer in the code - which can be done faster if you sort comments by answer id, etc.



You can also cache memcached files, files or others, which should speed things up.

Of course, creating objects from structures like this can be more complicated than just per request, but this is something you just have to do.

+1


source


Jani Hartikainen summed it up, I would like to mention something else.

Once you've learned OOP, things start to look like an object.

While it is true that everything can be declared as a class and treated as an object, you don't need to. In your example, this is probably easier to think of as bulk data manipulation rather than dealing with individual question / answer / comment objects.

Once you look at it from this angle, you are likely to come up with solutions like Jani.

0


source







All Articles