Fetching Database Rows with a Model for the CodeIgniter PHP Framework - The complete source code of the generic model class
(Page 4 of 4 )
As I said in the previous section, below I listed the full source code corresponding to the generic model class, this time including the “fetch()” method that you learned before. 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
$this->errors[] ='No rows were returned by the query.';
return FALSE;
}
}
For the time being, the model class looks a bit more functional, since it’s capable of fetching one and multiple rows from the specified database table. Nonetheless, there’s still a long road ahead of us, because it’s necessary to code other methods that provide the class with the ability to insert, update and delete database records. But, these will be coded in upcoming tutorials, so for the moment play with the class in its current state and try to incorporate into it your own improvements.
Final thoughts
Over this third installment of the series, I coded a core method of the generic model class, which comes in handy for retrieving records from a specified database table according to a number of conditions. This is a typical wrapper for many of the query modifier methods provided by CodeIgniter’s Active Record class, so I suggest that you first familiarize yourself with its underlying logic.
In the upcoming part, I’m going to define another core method for the model, which this time will be responsible for inserting new rows into the associated database table. Therefore, don’t miss the next article!
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.