Welcome Guest, Not a member yet? Register   Sign In
When do I use the model?
#1

[eluser]johnnyForums[/eluser]
After viewing the tutorials I'm not sure when to use models instead of controllers for database access. When do I use the models? Thank you.
#2

[eluser]seanloving[/eluser]
I put my basic CRUD functionality into model file(s) so that I can interact with my database from any of my controller files.

I design my controllers to first interact with the data model (if required) and then to call a view file that will generate a new page for the user.

For example, sometimes my controller receives user-submitted data. If the user-submitted data needs to be stored in a database, then my controller first calls a write function (located in a model file) before calling the view file that tells the user "success", etc.

If you are not yet up to speed on basic MVC... then search the net. Just a little reading will go along way. Good luck.

--Sean Loving
#3

[eluser]Yorick Peterse[/eluser]
[quote author="johnnyForums" date="1248379064"]After viewing the tutorials I'm not sure when to use models instead of controllers for database access. When do I use the models? Thank you.[/quote]

Whenever you are handling data. Models aren't ment to be used just for database related stuff.

The controller : Loads things, validates, etc
Model : Executes things, such as queries
View : Displays the results
#4

[eluser]johnnyForums[/eluser]
How come they only use the controller and view in the tutorial? simplicity?
#5

[eluser]Zack Kitzmiller[/eluser]
[quote author="johnnyForums" date="1248384099"]How come they only use the controller and view in the tutorial? simplicity?[/quote]

Exactly, and to show the flexibility of CI in general. You can really do anything any where. Anytime you're dealing with data you use a model.

Calculations, DB Queries, Reading Files, Writing Files, XML, etc... any sort of wrangling belongs in a model.




Theme © iAndrew 2016 - Forum software by © MyBB