Building a Generic Model for the CodeIgniter PHP Framework - The new generic model class
(Page 4 of 4 )
As I stated in the previous section, it'd be really helpful to list the complete signature of the generic model class in its current state, so you can see at a glance how it looks so far. With that idea in mind, below I included the class in question. Here it is:
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
* Gets value of primary key of the associated table for the model
*
* @author Alejandro Gervasio
* @return integer
* @access public
*/
public function getID()
{
return $this->id;
}
}
Admittedly, the generic model class in its current state isn't very functional, but as I expressed in the introduction, this a progressive development process, meaning that there's still a long way ahead of us. Of course, the class needs to implement several methods that will be used for performing at least some CRUD operations against its associated database table. However, these will be incorporated in forthcoming articles of the series. So, be patient for the moment, and meanwhile, don't forget to save the model under the /system/application/libraries/ folder of your own CodeIgniter installation.
Final thoughts
In this introductory chapter of the series, I started building an abstract model class that extends the native default model provided by CodeIgniter. Even when at this time, my class in pretty useless, I'm going to provide it with more functionality in upcoming parts of the series.
Precisely, in the next article I'm going to define and implement a few other methods. We'll use these methods for setting the validation rules for the generic model, as well as for storing the data in the form of an array that will be utilized for inserting and updating rows of its associated table.
Now that you know what the following article will be about, you can't 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.