Welcome Guest, Not a member yet? Register   Sign In
Noob question about MVC
#1

[eluser]yankeyhotel[/eluser]

If I have a date variable that is being pulled from several inputs in a form. And I want to add them together (or manipulate them) before I put them in the database. Should that manipulation happen in the Model or the Controller?

Also, if someone knows of a good tutorial discussing MVC and CodeIgniter, let me know.

Thanks
#2

[eluser]solid9[/eluser]
You capture the date in the controller.
You can manipulate it in controller or model.
It does not matter.

But remember all Database Query must be in the model.
#3

[eluser]yankeyhotel[/eluser]
purrrrrrfect Smile

thanks again solid9!
#4

[eluser]PhilTem[/eluser]
To get a little more precise to answering your question:

You may of course manipulate the data before passing it to your model and inserting it to the database. You may as well run the DB query directly within the controller. Even though both of these approaches aren't real MVC Wink

It's probably best practice to put all business logic for DB queries into your model also changing the data. This way you can use the model's methods in any controller and don't have to put the data changing logic into each controller (if you need to update the data with more than one controller).

Usually the controller only acts like: Which action is requested by the user so which actions do I (the controller) have to perform. For example get any data from the db and load a appropriate view. Personally, I think the controller is more a negotiator between libraries/models and views. It won't manipulate data directly but maybe by calling any libraries/models' functions.
#5

[eluser]yankeyhotel[/eluser]
@PhilTem Thanks for the reply, very helpful stuff.

I have another related question. Should a Model be specific to a controller? So if I have a model that grabs all of the entries for a blog, and have that being called from a couple of different controllers. Or should I have a model for that one controller.

If I understand it correctly, it would make sense to have a Model for each specific task you would need to interact with the database, and just use that in every controller you need it in? So then would it be ok to have multiple models load in a controller, right?

Also, do you need a controller for each view?

thanks again for helping the new guy with lame questions. Tongue
#6

[eluser]PhilTem[/eluser]
First of all: You don't need a controller for each view. You need a method for each view and a controller to "bundle" these views. Maybe you should read into MVC before developing apps with it. Don't wanna sound rude, but it makes life so much easier to know the basics and how the work and interact before using them Wink

Second: My personal opinion is to use one model for one table. I see a model as a layer between PHP and the database. So whenever I need information from the database, I use the corresponding model.
Hence a model is specific to data and not a controller (since a controller is only specific to an URI/task)

BUT: Whenever it comes to using more than one table for retrieving data within you controller, I tend to using a library which bundles hierarchical calls to models and sorts and creates my data properly.

For your example, if you created a blog controller which will display blog posts and all their comments I would actually create one controller with a remap function (to easily distinguish between single posts pages and categories, tags, ...).
Furthermore there would be a library that either grabs all posts (plus additional data like comments counts, metadata, images, ...) or that either grabs a single post with all its comments. Since posts and comments should be stored in different tables you'd have to get data from two models within the controller. Or you'd easily make the controller call your library which does all tasks.
Additional advantage: You have one place to add/modify the code (the library) and can reuse it on other controllers/pages as well.

To add: This is just my usage/understanding of MVC and CodeIgniter after using it for a little more than one year now (and haven't heard of MVC before Wink ).




Theme © iAndrew 2016 - Forum software by © MyBB