CodeIgniter Forums
basic but.... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: basic but.... (/thread-6780.html)

Pages: 1 2


basic but.... - El Forum - 03-11-2008

[eluser]soupdragon[/eluser]
going mad nonetheless...


I am still having problems wrapping my head round CI / MVC

For example i have a main page -

where i want a log-in box at the side
a navigation at the side
a header - which can and cannot include various js bits and pieces

the actual page showing what i want

and then the footer.

Not too unusual i suspect - and not using CI one i would have no problems making(including my classes, base files etc.

But in CI.....

i have now under views/common/ sidebar, header, footer - fine no probs

main page i have whatever view with a controller for it which then loads the views header, footer, sidebar, and itself - (now in every main page controller i have to set my css, my base so i can pass it to my various views - (code duplication cos my css and base stay the same but thats beside the point))

......but the login box ? - i have a controller for it but dont want to call just the login
so how do i combine 2 controllers (have read about various models) i suspect i am just having a mental blank here but help...
should i move the whole login to a model, a library or what

If someone can point me in the right direction using simple terms i would be grateful..


basic but.... - El Forum - 03-11-2008

[eluser]Pygon[/eluser]
the user login code is part of the model, interaction with it is a controller.

IE:

Model contains functions to determine if the user is logged in or not, has access, to log in, to log out, etc.

Controller is the model interface (confusing term but the "programming interface" not "visual interface") which relays the user actions (log in, log out) to the model.

Your view simply asks the model whether to show the login form or the logged in section and displays accordingly.

Load the view and away you go.


basic but.... - El Forum - 03-11-2008

[eluser]soupdragon[/eluser]
so move everything to various models - including validation etc

and then the controller just calls the various models and puts the results into the views


basic but.... - El Forum - 03-12-2008

[eluser]soupdragon[/eluser]
and then i return to the question - when for example i am making a contact / form page

where should i put all my rules, fields, validation ?
in the controller where it quickly becomes too much
or can i move it to a model - which then does the validation, sends mail etc and returns basically true or errors to the controller ?


basic but.... - El Forum - 03-12-2008

[eluser]adamp1[/eluser]
Validation should really go in the model, but it doesn't really matter. I currently have validation in my controllers but am trying to change that, makes it easier to change then.


basic but.... - El Forum - 03-12-2008

[eluser]xwero[/eluser]
If the validation is in the model, the model isn't view independent or you have to add the rules and fields configuration in the controller and pass that to the model. And how are you going to couple the form fieldnames with the db table fieldnames? I've been thinking about this of and on for some time now and i didn't find the perfect solution yet.
Code:
//controller
function somepage()
{
   $rules['username'] = "required";
   $rules['password'] = "required";
   $rules['passconf'] = "required";
   $rules['email'] = "required";
   $fields['username'] = 'Username';
   $fields['password'] = 'Password';
   $fields['passconf'] = 'Password Confirmation';
   $fields['email'] = 'Email Address';
   $dbfields['username'] = 'username';
   $dbfields['password'] = 'password';
   $dbfields['email'] = 'email';
   $where = array('id',$_POST['id']);
   $error = $this->somemodel->update($dbfields,$where,$rules,$fields);
   if($error == 1)
   {
      // success
   }
   else
   {
     if($error == 0)
     {
        // database error
     }
     else
     {
       // validation error
     }
   }
}
// model
function update($db,$where,$valrules,$valfields)
{
   // validation library loaded sooner
   $this->validation->set_rules($valrules);
   if(is_array($valfields))
   {
     $this->validation->set_fields($valfields);
   }
   if(!$this->validation->run())
   {
      return $this->validation->error_string;
   }
  
   foreach($dbfields as $dbfield => $dbvalue)
   {
       $this->db->set($dbfield,$this->input->post($dbvalue));
   }
   $this->db->where($where);
   $this->db->update('sometable');
   return $this->db->affected_rows();
}
This could work Smile


basic but.... - El Forum - 03-12-2008

[eluser]soupdragon[/eluser]
makes me feel a little less stupid at least :-)


basic but.... - El Forum - 03-12-2008

[eluser]xwero[/eluser]
We all live and learn here Smile maybe next week the solution will be different.


basic but.... - El Forum - 03-12-2008

[eluser]nmweb[/eluser]
[quote author="xwero" date="1205336187"]If the validation is in the model, the model isn't view independent or you have to add the rules and fields configuration in the controller and pass that to the model. And how are you going to couple the form fieldnames with the db table fieldnames? I've been thinking about this of and on for some time now and i didn't find the perfect solution yet.
...
This could work Smile[/quote]

If you're validating in the model you should declare the rules in the model as well. This keeps your code DRY as you don't have to copy the rules to another controller each time you use the model. The model stands before the data so any alteration to the data passes by the model and thus should pass validation.

Form fieldnames and table fieldnames problems can be covered by using similar names or use 'select name as username from table'

Akelos framework shows neatly how validation in models can be done.


basic but.... - El Forum - 03-12-2008

[eluser]xwero[/eluser]
What if the client decide they don't want a field anymore? Then you have to change your model but if another view depends on the same model with the original field in it, it will not be validated anymore. That is why i choose to add the rules in the controller instead of the model. If you use the same configuration with the same fields and fieldnames you can put it in a custom configuration file and load it so you don't have to copy the rules.

Similar fieldnames aren't always possible or wanted. For the other solution, how would you connect the form fieldname with the db fieldname? It has to come from somewhere.

I'm looking for the most reusable model methods so i think it's best the model is as independent as possible from a view.