Model Data and Validation Rules for a Generic CodeIgniter PHP Framework Model
(Page 1 of 4 )
Welcome to the second part of an eight-part series on building a generic model for the CodeIgniter PHP framework. In this part, I will expand the functionality of the “AbstractModel” class we created in the first part by adding a couple of handy methods to it. Keep reading for the full details.
If you’ve ever built PHP applications with the CodeIgniter framework, then you know how useful it can be for speeding up the whole development process, due to its powerful capabilities and flat learning curve. What’s more, its solid implementation of the Model-View-Controller pattern permits you to quickly separate data and application logic from visual presentation, thus facilitating creating programs that are maintainable and modular as well.
However, all of these advantages can be empowered even further with the existence of a single highly-functional generic model class that allows you to perform CRUD operations against multiple database tables through a group of simple sub classes. So, if you’re interested in learning how to build a model like this, then you might want to look at this series of articles. In it, you’ll be provided with a comprehensive guide that will walk you through this process in a step-by-step fashion.
In the first part of this series, I proceed to create the basic structure of a generic model class called “AbstractModel.” It defined some straightforward methods (apart from the constructor) for setting and retrieving the value of the “id” field corresponding to the database associated with the model.
Nonetheless, the model class in its current state is pretty useless; we need to define a few more methods that make it a bit more functional. Therefore, in the next few lines I’m going to code a couple of brand new methods. The first one will be responsible for setting the data the model will take for performing insert/updates operations, and the last one will be tasked with configuring the validation rules that will be applied to this data.
Now, it’s time to leave the theory behind and start writing these methods of the generic model class. Let’s jump in!