开发者

Help with Codeigniter and MVC model pattern

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 do开发者_运维百科es 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?


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


Do not make a model for the joins. As answered by @Johnny already, a Model and a table do not need to have a one-to-one relationship. In this case you are displaying blog entries, so you could have a Model named "Blog", with a method "GetList()". It is not relevant whether that query reaches out to multiple tables.

Think about it conceptually. Your are displaying blog entries, and each blog entry has other objects associated to it (such as a user id). Try to think domain-driven, not table-driven.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜