Building a Generic Model for the CodeIgniter PHP Framework - Defining the generic model's basic structure
(Page 2 of 4 )
The first step that I'm going to take to start building the generic model for CI will consist of defining its bare bones structure, including the declaration and initialization of all of its properties and main methods. Since my plan is based on extending the native model class provided by CodeIgniter, the generic model is going to simply be a sub class of it.
That being explained, here's the initial (and pretty skeletal) definition for my custom model:
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
protected $errors = array(); // model errors
/**
* Constructor
*
* @access protected
*/
protected function __construct()
{
parent::Model();
// get CI super object as a model property
$this->ci =& get_instance();
}
}
As shown in its initial state, the generic model is a class that extends the native CodeIgniter model. This is why its name contains the "MY_" prefix, which is mandatory within the CI namespace when extending a core library. Also, it's worth noting that the class has been declared abstract (this is only valid in PHP 5) since it shouldn't be accessed directly within any controller.
Now, focusing on the custom model's properties, you can see that it declares eight members, whose descriptions are pretty self-explanatory. First, the $table property will store the name of the database table associated with the model, and then the $fields property will contain an array of names corresponding to the fields defined for this table. Finally, the $id member will hold the value of the "id" table field to which the model points.
The other class members are very easy to grasp, but here's a brief description of what each of them does anyway: the $data variable will house an array of elements used for inserting/updating a given record into the associated table, while that $insertID will store the ID returned after inserting a new row in the table in question.
Lastly, the $numRows member not surprisingly will store the total number of rows returned by SELECTS statements, and the $validation and $error properties will contain the validation rules set for the custom model and the eventual errors triggered by it respectively. Not too difficult to understand, right?
Now that you hopefully grasped the meaning of each property declared for the custom model, it's time to pay attention to its constructor. In this specific case, its signature is quite short since it first calls the constructor of the native model class, and then grabs an instance of the CI super object, which will be used afterward to work with some native libraries included with CodeIgniter.
So far, so good. At this point you should have a fairly clear idea of how the custom model is going to work, mostly based on the description of its properties. However, this is only the beginning; the next step will consist of adding a few simple setters and getters methods to the generic model that you saw before.
To see how these methods will be defined, click on the link below and read the following section.