Welcome Guest, Not a member yet? Register   Sign In
MVC convenctions - how does it work?
#1

[eluser]yarek[/eluser]
Hello,

I'm a beginner to Code Igniter and MVC so I ask. Lets say i want to build an application named "cms" (just testing). I create cms_model and cms_controller. I want to put some news on a main site so I create entry_model and entry_conroller. Now I need to join these two things and I do something like this:
1) I load entry_model within my cms_controller and use it to get news from a database
2) I use entry_controller just to insert and modificate entries stored in a database.

Is it wrong? Did I break the rules of MVC patterns? I just can't get how to join all my application pieces and use it in one controller. The other idea I thought was: I create only one controller and few models and I use it only in that one controller. What do you think?
#2

[eluser]nmweb[/eluser]
You have several controllers for related functions, a controller for user stuff, a controller for article stuff ,etc.

Apart from that you have models, often one model would represent one type of data, say a table in your database. Or news-stuff stored in text-files. Etc..

You can happily use a model in several controllers, its recommended even as it promotes DRY. Your frontpage e.g. needs user-related,news-related,comment-related data, you can load a model for each one, in one controller.

Keep in mind that models do the retrieving from the database, you kindly ask the model to give you the information.
#3

[eluser]ejangi[/eluser]
Hi Yarek, welcome to the forums! I posted a reply to a previous similar query late last year. I hope it helps.
#4

[eluser]Vik[/eluser]
The way I like to think of it is...

-- anything that accesses a database goes in the Model

-- anything that puts text on the screen goes in the View (and the view can't contain any database calls)

-- the code that calls/controls/coordinates the View and the Model, goes in the Controller.
#5

[eluser]yarek[/eluser]
Hmmm. Now I understand, I've thought that *every* model has to have his own controller and it's not true, right? I'm building really simple cms so that you can get access to the everything from the main page and here comes my idea: one front controller and few models Smile Thanks a lot for help guys! Smile
#6

[eluser]Phil Sturgeon[/eluser]
Not every controller needs a model, but every database table should have one. Each one should have a basic, get, get list, insert and update function, then add some more specific ones as required.

Also, models are not just for database. The whole point of a model is that it is a data interaction layer and grabs your data from wherever the hell it wants without you caring. This is why nobody should return $query, but should return $query->result_array() for example. That way you can use XML as a source if you wanted or swap it for ANYTHING! Otherwise its not really taking advantage of the extra layer.
#7

[eluser]Dr.Dan[/eluser]
Hi thepyromaniac,

But it would be more flexible if i return just $query from model, so that I can use it anyway I like in view for eg: $query->result() or $query->result_array(), or even $query->num_rows(), all these using just one query? Whats your view?
#8

[eluser]Phil Sturgeon[/eluser]
I personally detest sites that cant make their mind up between result() and result_array(). I like to stick with one and make all developers use it, making it part of the model return seems a good way of doing that.

Anyway it can all be achieved using simple PHP. For example...

Code:
$data = array('
// Uses result() to get a list of objects to the view
'comments' => $this->comment_model->getComments(),

// If you are using results() like above but want an array... here you go!
'comments_array' => (array) $this->comment_model->getComments(),

// How many were returned?
'total_comments' => count($this->comment_model->getComments()),

// Wanna stop being lazy?

'proper_count' => $this->comment_model->countComments()
);

This should show you a few ways of handling things. The second example is obviously not something you would chose to go with, but is more of a hack to get around certain problems. For example I always use result_array() in my models, but one of my developers used result() and coded thousands of lines of views and whatnot using that. I wanted to use the same model but have array return, so changed the variable type.

Gotta love the flexibility of PHP over something like C++. Cannot convert char(*) to string >.<




Theme © iAndrew 2016 - Forum software by © MyBB