Since the custom model class should have a loosely-coupled structure, it must be provided with the ability to recursively autoload a specified model without having to rely on other classes. Therefore, it’s necessary to define a method that performs this task. With that requirement in mind, below I included the definition of a brand new method, not surprisingly called “autoload(),” which includes a specified class by using a combination of a simple “require_once()” PHP function and the RecursiveIteratorIterator SPL class. Here’s how the method looks: // Autoloads recursively child models required by the Abstract Model class public static function autoload($model) { // Don't attempt to autoload CI_ or MY_ prefixed classes if (in_array(substr($model, 0, 3), array('CI_', 'MY_'))) { return; } // Set path and model $path = APPPATH . '/models/'; $model = strtolower($model) . EXT; // try to include recursively the model file $rit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); foreach ($rit as $entry) { if ($model === $entry->getFileName()) { require_once($entry->getPathname()); return; } } show_error('Model class not found.'); } As you can see, the previous “autoload()” method will search recursively within the “models” folder a specified model and attempt to load it via the RecursiveIteratorIterator SPL class. If the model is found, it’ll be dynamically included by means of a “require_once()” statement. Otherwise, an error will be triggered by using the “show_error()” proprietary function of CodeIgniter. In addition, it’s valid to point out that the implementation of the “autoload()” method is based partially on the DataMapper library developed by Simon Stenhouse. However, I’ve improved and shortened its signature by using the SPL library. Having already explained how the custom model is capable of autoloading a specified class, it’s time to define another method, which will validate the data required for inserting and updating a database row. This new method, called “validate(),” is protected, and its definition is shown below: // Validates model data protected function validate($data) { // If no validation rules were set trigger an error if (empty($this->validation)) { $this->error = 'No validation rules were set for the model.'; return FALSE; } // Load CI validation library $this->ci->load->library('validation'); // Load CI language file for validation $this->ci->lang->load('validation'); // reset error messages $this->error = array(); foreach ($this->validation as $field => $rules) { $exprules = explode('|', $rules); // if the field is not required check next one if (! in_array('required', $exprules, TRUE)) { continue; } // Iterates through the validation rules foreach ($exprules as $rule) { // Removes the parameter from the rule (when specified) $param = FALSE; if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match)) { $rule = $match[1]; $param = $match[2]; } // Calls the validation method that corresponds to the rule if (method_exists($this->ci->validation, $rule)) { $result = $this->ci->validation->$rule($data[$field], $param); } elseif (function_exists($rule)) { // Tries to run a native PHP function if method of CI validation class doesn't exist $result = $rule($data[$field]); } // if an offending field was found store error message in error array if ($result === FALSE) { $this->error[] = sprintf($this->ci->lang->line($rule), $field); } } } return empty($this->error) ? TRUE : FALSE; } Despite its long definition, the above “validate()” method is quite easy to grasp. It simply validates the data that will be used when inserting and updating a database row by means of the native CodeIgniter validation library. If the method finds the inputted data to be incorrect, it’ll return FALSE. Otherwise, it’ll return a value of TRUE. Not too difficult to understand, isn’t it? Now that you hopefully grasped the logic that drives the previous “validate()” method, let me show you another one, which will build the corresponding error strings when the inputted data fails to pass the validation process. Here’s the signature of this method, called “get_error_string().” // Returns error string produced when performing validation protected function get_error_string() { $str = ''; $error_sufix = str_replace('<', '</', $this->error_prefix); foreach ($this->error as $error) { $str .= $this->error_prefix . $error . $error_sufix; } return $str; } Basically, all that the previous method does is return to client code a preformatted error string, based on the “error_prefix” property initialized at the beginning of the abstract model class and nothing else. So far, so good. At this point, I defined the remaining methods of the “AbstractModel” class that validate incoming data, autoload a specified model and finally create the corresponding error strings. Therefore, the last thing that I’m going to show you in this tutorial will be the full source code of the model class, so you can see at a glance how it looks after including the methods mentioned earlier. This will be done in the following section, so click on the link that appears below and read the next segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|