Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter and MVC
#1

[eluser]ech0[/eluser]
Hello there,

Along with this great framework, I'm introduced to MVC approach for the first time. I'm doing my best to keep myself on the right MVC path, but there are some things I don't quite understand.

Model is where all the interaction with database happens and Controller is a middleman between Model and View. I get that, but let's say I have something more than just getting stuff from database happening. I have a string consisting of a few lines, each line gets separated and then exploded into parts. And just after that I put the results of previous algorithm into the database. So where does the described algorith go to -- Model or Controller? Just to sum up, I don't quite get where should various calculations, manipulations and algorithms unrelated to database go. Could anyone clarify this?

Thank you in advance!
#2

[eluser]jedd[/eluser]
Hi ech0 and welcome to the CI forums.

Unsurprisingly, there are two schools of thought on this subject Smile

I'm of the do it in the controller school - I find it easier if I keep the business logic, for want of a better description, in the Controller - and use the model purely as a means of managing the data(base).

If I'm doing something that requires some data from a table handled by one of my models, and needs to update that or another table also handled by that model, and the action could be considered atomic, then I'll bundle it all in as part of the model function of course. I think there'll always be a degree of pragmatism required with such theory-meets-design questions.
#3

[eluser]_www_[/eluser]
Hello ech0,

well, that actually depends.

I'd probably keep the controllers to the least possible minimum and put the stuff working with the data into the model. This way the controller stays what its meant for (junktion between model and view).
You can reuse the model's function between controllers this way. In your example: maybe you have a place in your application where users can add the data in this way, but you also have a setup-Controller that initializes the database where you could use that to ease the pain of inserting the first bunch of data.

Greetings
#4

[eluser]jdfwarrior[/eluser]
Following the other guys..

The only thing it really depends on at that point, is what you want to do. You can do it either way. Which ever way makes more sense and makes things easier for you is the way to do it. You can process stuff in the controller if you want or you can split it out into functions in the model, your own libraries, or helper functions.
#5

[eluser]Developer13[/eluser]
This link has been posted elsewhere in the forums, but read here: http://blog.astrumfutura.com/archives/37...iated.html.

CodeIgniter treats models as a simple database access layer - when in reality they should be more than that, and the controller should not be treated as a "middle man" between the model and the view. There is nothing wrong with the view having direct model access. The controller should handle the flow and logic of the application.
#6

[eluser]Thorpe Obazee[/eluser]
http://forum.kohanaphp.com/comments.php?...=23&page=1

This is something insightful I've read in the Kohana forums.
#7

[eluser]NogDog[/eluser]
[quote author="Developer13" date="1239651116"]...CodeIgniter treats models as a simple database access layer - when in reality they should be more than that,...[/quote]
While I agree that the documentation seems to suggest this and it reflects what is probably the most common usage, I cannot think of anything in the CodeIgniter architecture that actually limits me to only using CI models as database access objects.
#8

[eluser]_www_[/eluser]
Hello everybody,

[quote author="Developer13" date="1239651116"]
CodeIgniter treats models as a simple database access layer - when in reality they should be more than that, and the controller should not be treated as a "middle man" between the model and the view. There is nothing wrong with the view having direct model access. The controller should handle the flow and logic of the application.[/quote]

Well actually a model should not be a database access layer. In my opinion it shouldn't be more than that either. Actually its different.
The model should describe what data you have and what relationships exist. Database stuff should be in an extra database access layer. That is in the best interest of the "seperation of concerns" principle.

dal - get the data from somewhere or put it somewhere (webservice, database, file, ... whatever)

model - just the business structure, objects with properties enforcing the needed constraints

controller - perform actions with model elements using the dal most of the time, pass model elements to the view

view - show the data
(Normally i would agree, that a view has access to model data. Its only common sence in this application structure.
But in codeigniter its mixed up with the data access layer. and i don't want to give a view the ability to interact with the database.
By the way, this is why all the examples do their queries in the controller and only pass the results of active records, isn't it?)

Greetings,
_www_




Theme © iAndrew 2016 - Forum software by © MyBB