Welcome Guest, Not a member yet? Register   Sign In
loading a model from within a custom library or helper file


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?


I suppose you could do it by getting the CI instance
$CI =& get_instance();
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.

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

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.


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
// get validation rules from model
function unique_username($username) {
  return $this->user->unique_username($username);
} // unique_username()

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

Now in your model
function validationRules() {
  return array(
} // 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...

Theme © iAndrew 2016 - Forum software by © MyBB