Welcome Guest, Not a member yet? Register   Sign In
Best practice for passing data from controller to model?
#1

[eluser]Unknown[/eluser]
Hi! I had a previous thread asking a similar question but I don't know where to find my old thread to extend on it.. so I'll ask here!

I've been searching for hour on best practices on models and one of my concerns is about how controllers pass post data to a model.

I've been thinking of 3 ways. My example is from my addUser method.

1. Assign variables in the controller that receive post data and pass them as parameters into a model method.
Code:
public function addUser()
{
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

  $post = $this->input->post();
  $username = $post['username'];
  $password = $post['password'];
  ...

      if ($this->user->addUser($username, $password))
                ....

2. Assign a data array in the controller with post data and pass it as a parameter into a model method.
Code:
public function addUser()
{
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

  $post = $this->input->post();
  $data['username'] = $post['username'];
  $data['password'] = $post['password'];
  ...

      if ($this->user->addUser($data))
                ....

3. Assign public properties of the model in the controller that receive post data.
Code:
public function addUser()
{
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

  $post = $this->input->post();
  $this->user->username = $post['username'];
  $this->user->password = $post['password'];
  ...

      if ($this->user->addUser())
                ....

In my own thinking:
- The first method makes the model reusable as it does not depend on the controller at all, as internally it will only know the parameters it receives. The parameters also help to document what the method is receiving.

- The second method is a bit cleaner, but then the model method will need to know the keys of the array passed by the controller in order to get the values, which makes the model less reusable and more dependent on the controller.

-The third method also makes the model reusable but not needing to know anything about the controller by having its public properties assigned. Also having class properties in the model seems more OO to me.

I'm curious what others best practices are regarding this?

Also, I come from a very OO background in C++ and Java, but only the basics, never with saving information to a file or DB. It seems models in CI are not what I would typically think of as objects, more just filled with CRUD methods. That's why the third method I listed seems more OO to me, as you assign properties. Anyone have any best practices towards structuring models in general?

I'm all very new to CI so thanks in advance for the help!





Theme © iAndrew 2016 - Forum software by © MyBB