Deleting Database Records with the CodeIgniter PHP Framework - The enhance version of the AbstractModel class
(Page 4 of 4 )
As I expressed in the previous section, it’s necessary to list the complete source code that corresponds to the “AbstractModel” class, this time including the definition of the “delete()” method that you learned before. So, here’s how this class looks after incorporating the new method:
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
There you have it. At this point, the generic model looks much more functional, since it’s capable of performing CRUD operations against its associated database table. That’s very, very exciting. But wait a minute! As you may have noticed, the “save()” method calls internally a private method called “validate(),” which should be responsible for checking the validity of input data before proceeding to perform an insert or an update operation.
As you probably guessed, I’m going to show you how this method will be implemented in the upcoming tutorial. In the meantime, feel free to tweak the class’s source code and have fun by introducing your own improvements.
Final thoughts
In this fifth chapter of the series, I demonstrated how to add a brand new method to the generic model class, which is tasked with deleting rows from the specified database table. However, I must say that the signature of the model is still far from complete, since it’s necessary to implement the validate()” method, which is called internally within the “save()” function.
In the forthcoming article, I’m going to create such a method, in this way providing the generic model with the capability to validate incoming data. So, want to see how this will be done? Then don’t miss the next part!
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.