CodeIgniter Forums
loading a model from within a custom library or helper file - 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: loading a model from within a custom library or helper file (/thread-4773.html)



loading a model from within a custom library or helper file - El Forum - 12-15-2007

[eluser]AtlantixMedia[/eluser]
hello,

was wondering..is it okay to load and access a model from within a custom library or helper file? or should calls to models be kept in controllers only?

thanks


loading a model from within a custom library or helper file - El Forum - 12-15-2007

[eluser]gtech[/eluser]
I suppose you could do it by getting the CI instance
Code:
$CI =& get_instance();
  $CI->load->model('whatever');
but not sure if its a good idea because libraries are supposed to reusable, and I personally don't think its 'normal' that they should be reliant on models or helpers being in place... however if it works for you.


loading a model from within a custom library or helper file - El Forum - 12-15-2007

[eluser]AtlantixMedia[/eluser]
no, I know how to load a model from a custom library. I wanted to know what's considered good practice in that regard


loading a model from within a custom library or helper file - El Forum - 12-15-2007

[eluser]AtlantixMedia[/eluser]
Can anyone suggest the proper way to design this? I don't like the idea of putting model calls into a library. this is for a registration system.

1) Register Controller handles form inputs.
2) Validation custom library. I decided to create my own validation library as I have other custom validation criterias to test in addition to the basic ones provided by the standard CI validation library. in addition I prefer to keep everything in one place. among the several validation rules, there are two which need to check whether a username and/or email are already in the database. this is where the model is loaded and accessed.
3) Auth_Model Model. Handles queries relating to input validation, login, new member inserts and updates.

It would be great if I could keep all model loads and calls in the controllers, but then I would need to also have the 2 custom validation functions in there which is not what I would like to do.

any help would be greatly appreciated.

thanks


loading a model from within a custom library or helper file - El Forum - 12-15-2007

[eluser]tonanbarbarian[/eluser]
Personally I dont really have much of a problem loading models in my libraries
But here is something else to consider.
If you use a callback in the standard validation library you can still check for unique usernames or email addresses

You need to create the callback function in the controller, or extend the validation class and put it there.
Then your callback function could call your model and get it to do the validation

sample code in method of controller
Code:
$this->load->model('user');
// get validation rules from model
$this->validation->set_rules($this->user->validationRules());
...
function unique_username($username) {
  $this->load->model('user');
  return $this->user->unique_username($username);
} // unique_username()

function unique_email($email) {
  $this->load->model('user');
  return $this->user->unique_email($email);
} // unique_email()

Now in your model
Code:
function validationRules() {
  return array(
    'username'=>'trim|required|callback_unique_username',
    'email'=>'trim|required|callback_unique_email'
  );
} // validationRules()

function unique_username($str) {
  // code to determine if the username is unique
  ...
  return (bool) $result;
} // unique_username()

function unique_email($str) {
  // code to determine if the email is unique
  ...
  return (bool) $result;
} // unique_email()

Now what I havent included in the code is that to properly determine if the username and email are unique you will need to know the id of the current user so that your queries are able to filter out the current record.
To do this simply set a property of the user model that is the id before the validation->run

Anyway this is a start...