Welcome Guest, Not a member yet? Register   Sign In
passing data to model
#1

[eluser]bigtimslim[/eluser]
What is the best way to pass data to a model. Maybe I shouldn't write best, but most common, or inline with MVC. I've seen it done these ways:

1.
Code:
function get_row($row_id)
{
    $query = $this->db->get_where('table', array('row_id' => $row_id));
    return $query->row();
}

2.
Code:
function addContact(){
  $now = date("Y-m-d H:i:s");
  $data = array(
    'name' => $this->input->xss_clean($this->input->post('name')),
    'email' => $this->input->xss_clean($this->input->post('email')),
    'notes' => $this->input->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
  );

  $this->db->insert('contacts', $data);
}

3. Settings the model class variables in the controller
#2

[eluser]Milos Dakic[/eluser]
I'd say up to you and how you want to organise your code. For me, I'd use the 2nd method.
#3

[eluser]TheFuzzy0ne[/eluser]
IMHO, data should be passed to the model pre-validated and sanitised, so it's ready to go straight into the database, or be handled by the model. None of my models access the post array, as I believe that's what the controller is for. The controller processes any input and decides what to do with it.

If there is a lot of code in my controller, I try to export it to methods either in the controller, or in a library. I'm sure some other people will disagree with me, however.

Hope this helps.
#4

[eluser]bigtimslim[/eluser]
Thanks for the fast replies, very helpful. Smile
#5

[eluser]JayTee[/eluser]
[quote author="TheFuzzy0ne" date="1236154855"]IMHO, data should be passed to the model pre-validated and sanitised, so it's ready to go straight into the database, or be handled by the model. None of my models access the post array, as I believe that's what the controller is for. The controller processes any input and decides what to do with it.

If there is a lot of code in my controller, I try to export it to methods either in the controller, or in a library. I'm sure some other people will disagree with me, however.[/quote]
Fuzzy is right on the money. As I've said in previous threads, I think that the model should be treated as if it's on a separate server. In effect, the model shouldn't have any awareness of anything other than database interaction.

As Fuzzy mentioned - I think it's a great idea to have some private functions in your controller to build/scrub your data before passing it to model.
#6

[eluser]bigtimslim[/eluser]
Cool. I think I will continue to do this how I have been then, but now I'm curious what you guys mean by seperate functions that build/scrub data before passing.

The only example in my head of srubbing before model is to run through XSS filter and that can just be done with the input->post option, or trim and I wouldn't create a separate function for that.

Can you give an example or elaborate? Smile
#7

[eluser]JayTee[/eluser]
[quote author="bigtimslim" date="1236156759"]The only example in my head of srubbing before model is to run through XSS filter and that can just be done with the input->post option, or trim and I wouldn't create a separate function for that.

Can you give an example or elaborate? Smile[/quote]
Sure :-)
Code:
//controller
function index()
{
  $this->load->model('user');
  //send the ready-to-load data to the controller
  $this->user->add_new($this->_build_user());
}

//private method inside controller
function _build_user()
{
  $user_data = array(
    'last_name' => $this->input->post('last_name'),
    'first_name' => $this->input->post('first_name'),
    'email_address' => $this->input->post('email')
  );
  //something random to show some other values that may
  //be set based on logic rather than the directly posted data
  if ($this->input->post('color') == 'blue')
  {
    $user_data['color_pref'] = 'Bluish Colors';
  }
  else
  {
    $user_data['color_pref'] = 'Non Bluish Colors';
  }
  return $user_data;
}
#8

[eluser]TheFuzzy0ne[/eluser]
That would constitute scrubbing, but you could use methods to scrub bulk input (say, 50 lines of input), and perhaps do some extra formatting, such as putting data into an array and doing any extra validation. An example of a function that could be it's own method might be a method that generates a password.

Also, I find that sometimes when I get huge blocks of ifs and elses, it's easier to export the content to separate methods to help maintain readability. Method names can be useful for describing what a block of code does, so you can know what's going on without having to see how everything works. For example $this->do_login() is more obvious that 50 lines of login script. If you really need to know what's happening under the hood, you can, of course, check out the do_login() method, but at a glance, you know what the block of code does, and you've only had to read a single line, not 50. I guess you could call it self-documenting code.

EDIT: Great example, JayTee!
#9

[eluser]Colin Williams[/eluser]
Quote:In effect, the model shouldn’t have any awareness of anything other than database interaction.

Make that data service interactions. Data could be from a database, a flat text file, a file directory, XML, external device, etc. I also think this extends to sessions. I used to have misgivings about avoiding using sessions in a model, but I've convinced myself that it absolutely makes sense for a model to interact with sessions, the only persistent data service available when developing on the Web.

Quote:I think it’s a great idea to have some private functions in your controller to build/scrub your data before passing it to model

To some extent this might be a good idea. There is a line to be drawn with how much the controller knows about the model of the objects it works with. The more forgiving your model is, the better. Most models I write can usually just get the $_POST array thrown at it, and it knows what values to ignore when performing any operations.
#10

[eluser]TheFuzzy0ne[/eluser]
[quote author="Colin Williams" date="1236157801"]Most models I write can usually just get the $_POST array thrown at it, and it knows what values to ignore when performing any operations.[/quote]

Sure, but they don't extract data directly from $this->input->post() do they? You actually pass it to them. I'm sure many people will say they are the same thing, so it doesn't matter, but I'd have disagree.




Theme © iAndrew 2016 - Forum software by © MyBB