Validating Input Data with the CodeIgniter PHP Framework - Listing the enhanced version of the generic model class
(Page 4 of 4 )
Undeniably, the best way to help you understand how the generic model works is by showing you its complete signature. With that idea in mind, below I listed the entire source code corresponding to the “AbstractModel” class, naturally including the recently implemented “validate()” method. Here it is for you:
abstract class MY_Model extends Model
{
protected $table = ''; // table associated to the model
protected $fields = array(); // fields of table associated to the model
protected $id = NULL; // value of the primary key of the table associated to the model
protected $data = array(); // model input data
protected $insertID = NULL; // insertion ID
protected $numRows = NULL; // number of rows returned by SELECTS
protected $validation = array(); // model validation rules
Despite the rather lengthy signature of the above model class, at this point its underlying logic should be pretty easy to grasp, since each of its methods have been discussed in depth in distinct articles of the series. Of course, the model can always be improved, so feel free to introduce into it your own enhancements, which will surely help you to sharpen your existing PHP skills.
Final thoughts
In this sixth episode of the series, I explained how to implement a protected method within the generic model class, which permits us to perform custom validation on the incoming data used for the model when it executes insert and update operations against its associated database table.
In the upcoming tutorial, things will get really interesting. I’m going to complete the development of the model class, and in consequence, you’ll have an opportunity to see for yourself how to use it in the context of a real MySQL-driven application. Thus, now that you’ve been warned about the subject of the next article, you don’t have any excuses to miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.