Hello there
I am using an objected oriented MVC framework in PHP (Kohana) and have kind of mashed together a few techniques to get stuff done. Problem is I am not sure how to keep things clean without calling lots and lots of queries per page.
To illustrate my example, I'll imagine I am designing a stack overflow like site:
I have model class for a question (question_model
) as well as one for question finder (question_finder_model
).
question_model mainly just contains variables to store the 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 containers an array of question_model objects as well as an array of question ids. The array of ids is populated by find methods in the class 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 questions objects.
The problem is that it is getting quite hard to handle, for example my question contains many answers and each answer can contain many comments and so on. How can I populate all these objects without having several queries. I mean the easy way would be to just iterate through my array of question objects and call a function stored within the question class which gets the answers info for that object. But then I would be calling 10's or 100's of queries per page.
I apologise if this is all abit hazy as the problem is quite difficult to articulate. Any help is appreciate because perhpas my entire pattern is flawed.