Model Data and Validation Rules for a Generic CodeIgniter PHP Framework Model - The enhanced version of the generic model class
(Page 4 of 4 )
As I promised in the section that you just read, below I included the complete source code that corresponds to the enhanced version of the “AbstractModel” class, to give you a clearer idea of how it looks after implementing the “setData()” and “setValidation()” methods that were defined before. 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;
}
/** Sets input data for the model
*
* @author Alejandro Gervasio
* @param array
* @return void
* @access public
*/
public function setData($data)
{
if ( is_array($data) AND count($data) > 0)
{
foreach ($data as $key => $value)
{
if (array_search($key, $this->fields) === FALSE)
{
unset($data[$key]);
}
}
$this->data = $data;
}
}
/**
* Sets validation rules for model data
*
* @author Alejandro Gervasio
* @param array
* @return void
* @access public
*/
public function setValidation($validation)
{
if ( is_array($validation) AND count($validation) > 0)
{
foreach ($validation as $field => $rule)
{
if (array_search($field, $this->fields) === FALSE)
{
unset($validation[$key]);
}
}
$this->validation = $validation;
}
}
}
As you can see, now the class is starting to look a bit more solid. Naturally, at this point the model is far from a mature state, but keep in mind that there’s still a long path ahead of us, and I’m going to incorporate more functionality into it. In the meantime, feel free to introduce your own improvements to the model class, which will surely be an instructive and why not, fun experience.
Final thoughts
In this second episode of the series, I expanded the functionality of the “AbstractModel” class by adding a couple of handy methods to it. In this particular case, the first one was charged with assigning an array of input values to the $data property of the model, and the last method was tasked with performing this same process, but with the $validation array.
In the next article, things will become even more interesting, since I’m going to code one of the core methods of the model: the one that will be responsible for fetching records from the associated database table.
Now that you’ve been warned, don’t miss the forthcoming tutorial!
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.