ORM, MVC, and PHP Frameworks. Acronyms FTW!

Lately I’ve been doing a lot of projects using the CodeIgniter framework. It saves me a lot of the setup code involved in a new project, and allows me to just dive right in and build an app. Fun. Unfortunately, starting a project, while easier, still requires re-writing a lot of the same code.

CodeIgniter enforces the MVC (Model - View - Controller) design pattern. Models can be thought of as tables in a database. Each model has an associated series of data fields. An address model might contain fields like street, city, state, and zip code. Additionally, models have methods with which to perform actions with the data. Still with me? All user input goes to a controller. The controller parses the input, and performs operations utilizing the various models. The model pulls data from the database, saves to the database, or simply does some computation. Data returned by the model is then passed, by the controller, to the view, which displays the data in a usable way.

Pretty straightforward, right? The problem is that most of the models I have been working with are the same. Which means that most of the code is the same. I’ve grown weary of re-writing the same c.r.u.d. methods (create, read, update, delete) for every model. Moreover, I’ve grown weary of updating both code and database whenever I want to edit the structure of an object. So, I have decided to try my hand at a simple ORM-inspired class that will handle these functions for me. That’s right nerds. It’s project time.

PROJECT TIME!
(Heck Yeah!)

Project goal: Write a simple php class that will

  • Allow me to design simple objects for use in projects
  • Take care of the c.r.u.d. of said objects
  • Generate HTML forms to edit the objects (This may be just laziness on my part, but I’m sick of writing forms.)
  • Update DB tables when the structure of the object changes.
  • Allow me to build custom methods for each model

Total work time: I’m thinking it would take a few hours, minus the database updates. Automatic DB updating would be as cool as Hoth, but I think it would be the most time consuming, and frankly the redundancy of updating code and database simultaneously doesn’t bother me. For now.

Anyway, if I actually follow through, I’ll post the code. If not, we’ll always have the memories of a php class that could have been.

-Fk