Welcome Guest, Not a member yet? Register   Sign In
MVC issues...trying to avoid interacting with the database from my controller
#4

[eluser]Buso[/eluser]
Write a generic method, something like: get_field_by($wanted_field,$where_field,$where_field_value)

eg:
Code:
$users_ids = $this->user_m->get_field_by('id','registration_date <',time()-60*60*24*30); // gets all the id's from users that has been registered for longer than 1 month

You could also add other already existing methods from the database class, to yor base model, like 'where()'. Or just add all of them:

Code:
public function __call($name,$arguments) {
  call_user_func_array(array($this->db,$name),$arguments);
  return $this;
}

I haven't tested the code, but you get the point. The idea is to be able to do this from your controller:

Code:
$this->user_m->where('role !=','admin')->get_accounts(); // where() doesn't belong to user_m, it was created dinamically

Or if you are in a hurry, you can do this:
Code:
$this->db->select('id,name,role')->where('role !=','admin');
$this->user_m->get_accounts();
Don't worry about 'breaking MVC', as long as get_accounts does all the rest. The problem comes when you start writting foreach's and stuff like that in your controller, just to add an extra field to the results.


Messages In This Thread
MVC issues...trying to avoid interacting with the database from my controller - by El Forum - 09-24-2010, 03:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB