views:

46

answers:

3

I am creating a site much like a wordpress blog whereby the front page will display a post loop: with the post_summary, author info, and tags.

I have four tables:

posts | users | tags | tag relationships

to display all the results i would need to do multiple JOINs for in the SELECT statement

However, to stay with the MVC pattern, there should be a model for each table ( or object ?). So my question is: If I were doing a SELECT all, how would I do this and still keep with the MVC pattern?

To get all the required info for the post, I need the author_id to get my info from the users table AND I need the post_id to get the tags (and so on). If all of my queries are in different Models, what is the best way to perform the query?

Do I make one Model that does all of the JOINS and just use it? Should I load Models from the view? Or should I do additional query work in the Controller?

+1  A: 

Make a model for the JOINS. It can include post_summary, recent_comments, etc.
Just use it in the front_page controller, the side_bar controller (for recent_comments, etc).

It would be better not to put query work directly in views or controller and views should not need to access the models IMO.

Sujoy
+1  A: 

I think you have a misunderstanding of the purpose of Models. Models are to deal with data in your database and are not limited to 1 table per model. If you are creating a blog, you will really just need one model. Take a look at the tutorial on the codeigniter website, http://codeigniter.com/tutorials/watch/blog/, and reread the user guide for models, http://codeigniter.com/user_guide/general/models.html . You may be getting MVC confused with an ORM

Johnny Tops