CodeIgniter Forums
why use models? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: why use models? (/thread-37051.html)



why use models? - El Forum - 12-25-2010

[eluser]jtan[/eluser]
Why do we need to use Models when we can put all the functions and organize them with just Controller's functions?


why use models? - El Forum - 12-25-2010

[eluser]bl00dshooter[/eluser]
[quote author="jtan" date="1293297848"]Why do we need to use Models when we can put all the functions and organize them with just Controller's functions?[/quote]

Why do we create lots of controllers when we can just create one? Why use MVC when we can do fine without it? Why...?

Because it's better this way. It's more organized and easier.


why use models? - El Forum - 12-25-2010

[eluser]jtan[/eluser]
not that, you misunderstood me.

but so far the benefit i can see with using models is you can share the data across different views. is there any others?


why use models? - El Forum - 12-25-2010

[eluser]jedd[/eluser]
[quote author="jtan" date="1293317119"]is there any others?[/quote]

jtan - you might want to read through the [url="http://en.wikipedia.org/wiki/Model–view–controller"]wikipedia article on model-view-controller[/url] architecture to get some good insights.


why use models? - El Forum - 12-27-2010

[eluser]jtan[/eluser]
with lots of objects going around when adding the model method, is it common practice to have a separate sheet to list all the variables and objects?


why use models? - El Forum - 12-29-2010

[eluser]nuwanda[/eluser]
Models allow you to use the same data functions for many controllers. Each controller calls the same model method, perhaps with different parameters which then return data.

Without models you'd be writing the same data retrieval code in each controller. Major headache.

Of course, I guess it's possible to have an app where each controller method accesses unique data.


why use models? - El Forum - 12-29-2010

[eluser]SPeed_FANat1c[/eluser]
[quote author="nuwanda" date="1293631570"]Models allow you to use the same data functions for many controllers. Each controller calls the same model method, perhaps with different parameters which then return data.

Without models you'd be writing the same data retrieval code in each controller. Major headache.

[/quote]

Without model you could yet use libraries instead of models for the same goal.


why use models? - El Forum - 12-29-2010

[eluser]nuwanda[/eluser]
Of course you can use a library (a class).

In CI, a model is an extended class, just as a controller is. If you want to create a library(a class) and load it in a model or controller, it'll work just fine.

Frankly, in CI, I create classes simply for one of their most powerful reasons: encapsulation.