Decoupling models from the database: Data Access Object pattern in PHP

January 5, 2009 – 1:07 pm Tags: , , , ,

Nowadays it’s a quite common approach to have models that essentially just represent database tables, and may support saving the model instance right to the database. While the ActiveRecord pattern and things like Doctrine are useful, you may sometimes want to decouple the actual storage mechanism from the rest of the code.

This is where the Data Access Object pattern comes in. It’s a pattern which abstracts the details of the storage mechanism - be it a relational database, OO database, an XML file or whatever. The advantage of this is that you can easily implement different methods to persist objects without having to rewrite parts of your code.

I’m again going to use the programming language quiz game I wrote as an example. Since I initially wrote it to use Doctrine ORM directly, and both the old and new code are available, you can easily see how the code was improved.

Read the rest of this entry »

Seven Things

January 3, 2009 – 4:04 am

Oh no, another of these silly blog tagging games. I was tagged by Pádraic Brady, and the point is to share 7 random or weird facts about me.

Read the rest of this entry »

Sandboxing Rhino in Java

January 2, 2009 – 12:07 am Tags: , ,

I’ve been working on a Java app which needed Rhino for scripting. The app would need to run untrusted JavaScript code from 3rd parties, so I had to find a way to block access to all Java methods, except the ones I wanted. This would not be a problem if there was an easy way to disable LiveConnect - the feature of Rhino which provides java access to scripts - but there is no such thing.

However, after a lot of digging around, I finally found a way to do this without too much hacking. In fact, it can be done by just extending a few of the Rhino classes, and using the setters provided to override some of the default ones.

Read the rest of this entry »

Best of 2008

December 30, 2008 – 2:36 pm

Since the year is about to end, I think now is a good time to highlight some best things of 2008

Read the rest of this entry »

Reusable “generic” actions in Zend Framework

December 27, 2008 – 9:56 am Tags: , ,

Sometimes you will need nearly the same functionality in many actions. This can lead to unnecessary code duplication if you aren’t careful, and there’s been a couple of occasions on #zftalk, where people have been asking for a good practice to avoid this.

There are several ways to deal with this, such as moving the code into a separate function, or an action helper. But in this post, I’m going to introduce so called “generic actions” - parametrized, easy to reuse actions - which is an idea similar to django generic views

Read the rest of this entry »

Happy holidays

December 25, 2008 – 5:19 pm

Merry christmas to everyone, or if you don’t do xmas, happy <insert your holiday’s name here>!

Also, here’s a small challenge for you: draw santa in mspaint (or something else) - but do it with your eyes closed!
Read the rest of this entry »

What will you do during the holidays?

December 22, 2008 – 5:12 pm

I’ve been asked this a couple of times.. What will I be doing during xmas?

Since the people asking that aren’t exactly computer people, the usual non-nerdy answer I give out is that I’ll probably take it easy and relax - you know, the usual stuff.

But since this is the internet, and this is a programming blog…

  • I’ll be learning some Java
  • I’ll be writing something hopefully awesome in Java
  • I’ll be coding a website with Python and Django
  • I’ll also hopefully be writing some interesting blogposts
  • And maybe I’ll even do something unrelated to computers!

What are your plans?

JavaScript margin/block commenting system

December 20, 2008 – 5:22 pm Tags:

On Djangobook, they used to have this interesting commenting mechanism on their book’s pages:

Each paragraph in the book could have comments, by use of a JavaScript commenting system which would display the amount of comments on each paragraph when you hovered over them with the mouse. You could also click on the little bubble which displayed the count, and leave a comment of your own.

A while ago, Pádraic Brady was looking for a similar system. This got me thinking about it again… and of course I had to try implementing it myself!

Read the rest of this entry »

The problems faced by a common model interface in frameworks

December 17, 2008 – 8:31 pm Tags:

You sometimes see people asking why the Zend Framework (or some others) don’t provide a library for the Model part in MVC.

While ZF provides data access classes in the form of Zend_Db and related components, it doesn’t provide any concrete examples of how you would implement a model class. This often confuses especially beginners.

Providing a common base model, or an interface for implementing models, does have certain benefits, but it also has some problems…

Read the rest of this entry »

Packageizer is fixed

December 15, 2008 – 12:55 pm Tags: ,

The packageizer had some problems determining the dependencies for the newly added Zend Framework 1.7 packages, but the problem is now fixed.

The packageizer source-code has also been updated with the patch.