A Web App Based on a Model for the CodeIgniter PHP Framework - Inheriting the functionality of the abstract model class
(Page 3 of 4 )
Undeniably, all of the hard work that went into building a generic model class is worth it when the model is used for building a database-driven program. In this case, the example real estate application that I’m planning to build will utilize only one MySQL database table that will store some fictional properties, but imagine the time you’ll save if your application is going to work with multiple tables.
However, the best way to understand the benefits of having an abstract model class is by creating a sub class from it, which as I said before will retrieve, save and delete a few properties. The signature of this child model, which I called “Property_model,” has been listed below. Have a look at it:
As you can see, it's very short and easy to define a model class that interacts with a specified database table when you have a generic model at your disposal. In this case, the above “Property_model” class only sets within the constructor the name of the MySQL table with which it’s going to work, along with the validation rules that will be applied to the data used for inserting and updating properties. That's all.
Of course, the advantages in relying on an abstract model class will be seen more clearly when working with multiple database tables. In that situation, you’ll only need to define short and simple child models for each table. Anyway, the previous example should give you an appropriate idea of how useful a generic model can be for quickly building complex database-driven applications with CodeIgniter.
So far, everything looks good, right? At this point, you surely grasped the logic that drives the “Property_model” child model, meaning that it’s time to see how it can be used within a controller class to retrieve, save and delete properties in a truly simple fashion.
So, if you’re interested in learning how this controller will be built, read the section to come. It’s only one click away.