I know we are suppose to call models from controller not the view but i'm in a situation that i have to do this
here it is :
I have a bloging script and in each user home page i want to show 10 posts and 5 comments for each post
So my controller looks something like
function get_posts($user_id){
$blog_model = $this->load->model('bloging');
$posts = $blog_model->get_user_posts();
$this->load->view('blog' , $posts )
}
Each post could have many comments so i have to get them too
So in my view i have
foreach($posts->result() as $p ){
echo $p->comment;
/// getting comments
$comment_model = $this->load->model('comments');
$co = $comment_model->get_post_comments($p->id);
foreach($co as $co )
echo $co->comment;
//////
}
I now this doesn't work like this as i have to use a helper or library or use a static method in model to do this( i'm using codeigniter , it might be different for other frame works )
But what alternative do i have ?
I can do this in controller , after getting posts use a foreach loop and get each post comments , put all comments in a 2d array($c_array[post_id][it's comments array]
)
Send them to the view with posts and in the view for each post look into the array for it's comments and .......
But it seams complicated , redundant and slower
i already have 4 joins on the post query and 2 joins on the comments query it would be 7 if i join these two
and also there is different limit for select in two query I'm fairly new to mvc . what is the right approach in this kind of situations ?