Am I braking MVC pattern? |
Hello there. I am actually coding a web-application using CodeIgniter, Bootstrap and HMVC extension. I have some questions.
1. Am I breaking MVC pattern if I explore object using foreach loop in VIEW like this: VIEW: PHP Code: <tr> 2. Am I breaking MVC pattern if I use in CONTROLLER mysql select statement like below?: PHP Code: $data['comitee'] = $this->_custom_query('SELECT users.firstname AS name, users.lastname AS surname, comitee.conference_id AS cid, users.degree_id AS sctitle, comitee.comitee_name AS comitee_name, users.phone_number, users.skype_profile, users.email FROM comitee JOIN users ON users.id = comitee.user_id'); Thanks in advance
Yes.
Put the query in a model. Controller gets the result from the model, and sends it to the view. You can do a loop in your view, but you shouldn't be executing a query there. Execute the query in the model and pass back an object or an array to your Controller, then your Controller can pass that to your view.
Yes, controller should only receive the query results (array) from the model and pass it on to the view.
Like other stated No and Yes
Views: No but, try to keep your views as light as possible. simple Loops and basic if / then's. if ($has_records) { echo 'Sorry no records found'; } else { echo 'Has Records'; /* do loop or something */ } maybe throw in a count($array) == 0 or something is still pretty basic. just don't get carried away Controllers calling SQL - yes (bad mvc) That should be in your model. The controller is really just a router for the request. it then calls models and libraries as needed and passed the information to the view. Keep it DRY (don't repeat yourself) - For example if you wanted to change a tables column name or even the table name. Only the model should be worried about that. you shouldn't need to go to 18 different controllers to change the table name. If you build the query's in the model then you would only need to goto the model and change perhaps $tablename = 'foo' to $tablename = 'bar'; Hope that helps (11-19-2014, 04:36 AM)piotrekk4 Wrote: 1. Am I breaking MVC pattern if I explore object using foreach loop in VIEW like this:I agree with the other responses. 1.- Not breaking the pattern. You pretty much have to loop through an array or object to get the values into the HTML document. Just do the fewest calculations possible. For example, if you're creating a table with number and percentages, calculate the percentages in the controller and include them in the array you send to the view. That way, the view just displays things. 2.- Yes, breaking the pattern. All queries should be done in the model. (11-20-2014, 09:00 AM)dmyers Wrote: For example if you wanted to change a tables column name or even the table name. Only the model should be worried about that. you shouldn't need to go to 18 different controllers to change the table name. If you build the query's in the model then you would only need to goto the model and change perhaps $tablename = 'foo' to $tablename = 'bar'; I have a question about that. The methods in the model rely on an array you send. For example, to add a user to the your table of users, in your controller PHP Code: $data = array( and then in your model PHP Code: public function add_user($data) So if you changed the column "pwd" to "password" in the users table, you would have to change the controller too, wouldn't you? Or how would you avoid that? Honest question, as I'm kind of new to Code Igniter myself. (11-20-2014, 04:49 PM)RobertSF Wrote: So if you changed the column "pwd" to "password" in the users table, you would have to change the controller too, wouldn't you? Or how would you avoid that? Yes, you would. Theoretically, you could take all the values for your model as parameters, or as an indexed array, and it would abstract things further and you wouldn't have to go back and change the controller if you changed a column name. But I think that in most cases, the shortcut method of passing an associative array with column names adds clarity to one's code. I do see how it does not completely separate the database from the controller which I think is the source of your question. Of course you could always make your model look like this PHP Code: public function add_user($data) if you wanted to retain the ability to alter column names without altering the controller
|
Welcome Guest, Not a member yet? Register Sign In |