Lithium, Doctrine & doing it yourself

If you use CakePHP you’ll probably be aware (or you’re dead or on holiday or something…) that Gwoo and Nate have left the project and gone elsewhere. Sort of. Well actually they have forked Cake3 into Lithium – souped up CakePHP for PHP 5.3

Unfortunately I’m not going to be using it in the immediate future due to the 5.3 limitation, but I had a good look through the blog example and it looks like it would be a pleasure to work with.

Looking round the Lithium site I also came across Doctrine – an object relational mapper (ORM) system. I spent an evening reading the documentation and decided it was perfect for a project I’m working on at work, unfortunately the next day I discovered we aren’t up to the required PHP version yet (almost, but not quite) so on the back burner for now.

I’m building a small application (about 8 tables) to be integrated into an existing CMS system. The structure of the CMS is essentially front controller based with a great template engine and well developed plugin / module system. The new module I’ve had to develop just didn’t fit well into the existing code structure and so I have ended up writing a Model / Controller system within my module that utilizes the things like the DB library, routing, security, templating etc. of the parent application.

The setup is pretty straight forward – a base model and base controller extended by respective models and controllers for each table. The child controllers and models are mostly pretty thin – mostly just setting class specific instance variables and the calling methods in the base classes with parent:: There isn’t any fancy ORM or anything like that in the models, just good old fashioned SQL, one query or closely related group of queries per method.

It took me about 1 1/2 days to write the framework and about another half day to write the templates (I’ve still got a load of reporting etc. to write) but now I’ve got the views for all the CRUD operations I will need and a lot of reusable code. Thinking about it, I would probably have done about a quarter of the work, in a far less maintainable way if I hadn’t taken this route. Goes to show that as long as your goals are well defined – this kind of approach far from being overkill has saved a huge amount of time and produced very understandable code – and you don’t need an entire framework to do it (with the added advantage that if you write stuff yourself, you hopefully have a better grasp of what is going on).

(I’ve still written a fair amount of Cake code this week :))