Welcome Guest, Not a member yet? Register   Sign In
Am I braking MVC pattern?
#1

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>
    <
td>Example Title:</td>
    <
td>
    <?
php
        
foreach($sciences->result() as $row7)
        {
            if(
$row->id == $row7->cid)
            echo 
'&bull; <strong>['.$row7->ftitle.']</strong><br/>';
        }
    
?>
    </td>
</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
Reply
#2

(This post was last modified: 11-19-2014, 08:28 PM by bclinton.)

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.
Reply
#3

Yes, controller should only receive the query results (array)  from the model and pass it on to the view.
Reply
#4

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
Reply
#5

(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:

2. Am I breaking MVC pattern if I use in CONTROLLER mysql select statement like below?
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. 
Reply
#6

(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(
 
 'login' => $user_login,
 
 'pwd' => md5($user_pwd),
 
 'email' => $user_email);
$this->users_model->add_user($data); 

and then in your model
PHP Code:
public function add_user($data)
{
 
 $this->db->insert('users'$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.
Reply
#7

(This post was last modified: 11-21-2014, 09:57 AM by bclinton.)

(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?

Honest question, as I'm kind of new to Code Igniter myself.

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)
{
 
 $db_insert_data = [
 
     'login' => $data['login'],
      'password' => $data['pwd'],
      'email' => $data['email']
 
  ];

 
 $this->db->insert('users'$db_insert_data);

 
if you wanted to retain the ability to alter column names without altering the controller
Reply
#8

Thanks, and nicely clarified!
Reply
#9

Thanks for the answers. That's all I wanted to know.
Reply
#10

@Robert
Mote elegent way is to fetch post or get data in your model using ci's input library . So you dont have to pass data from controller.
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull
Reply




Theme © iAndrew 2016 - Forum software by © MyBB