I have been developing web applications lately using Codeigniter framework, have also worked with Opencart for a few years. I've read on MVC implementation in PHP and understand that there is a lot of disagreement regarding the purity or correctness of the MVC pattern as applied in various PHP MVC frameworks. I want to ask the more experienced guys here what would be the "more correct" way to deal with the following situation.
I have a database with arbitrary data, say table_1, table_2, .table_3 that each have a slightly different schema.
I strive to make my SQL queries as generic as possible, so I use variables like this:
SELECT * FROM '.$sTable.' WHERE '.$sUserRefCol.' = 123
I also store each table specific information in configuration array, like
$config['db_map'][1] = array(
'table' = 'table_1',
'user_ref' = 'seller_id',
'cols' = array(...)
);
So, based on user input (POST parameter), I get DB variables for specific section. I am not sure whether this "mapping" should happen in the controller or I can do it in model.
Right now, I do this in the controller, like:
$section = $_POST['id'];
$db_map = $config['db_map'][$section];
$sTable = $db_map['table'];
$sUserRefCol = $db_map['user_ref'];
...
Then I pass all these to the model method, like:
$this->my_model->get_data($section, $sTable, $sUserRefCol);
So, my question is: Could I just pass the $section id to the model and let it handle the loading of the configuration?
Another example, instead of POST parameter, say we have a session variable pertaining to user section or department or access rights etc.
Should I read these values in the controller, then call the appropriate model method with appropriate parameters? Or I could just do a straight call to
$this->my_model->my_smart_method();
that would read the session vars and perform appropriate operation?
Well thank you for a brief question. First of all, let me clear about one thing, controllers do not interact with database or table, even they dont know which table you are going to use etc. All a controller know is its relative model. I guess if you study a little deep, it will clear up all your confusions. I found and read a great article about MVC pattern which is outstanding, here is the link.
Your controller should know nothing about your database. It simply asks for data from a model. For example, if might ask for a list of users from a user model. The only thing it knows is that it needs a list of users. Where and how that list is stored is the model's problem. The list of users could be in a database, on some other website, in a flat file, etc... The controller orders a chicken. The model is the kitchen. The controller has no idea where the chicken was bought, how it was prepared, etc... It just orders a chicken and gets a chicken.