PHP: a simple model mapper with Zend_Db_Table

Using Zend_Db_Table to access database is a good way to retreive data. However, the pattern Zend_Db_Table implements (table data gateway) has, IMHO, a major drawback: it does not provide abstraction of table’s fields names.

This article describes a simple solution that I use…

Introdution: the problem

Providing tables’ fields names abstraction is sometime also called “row mapping”. The purpose is to avoid using column’s name in the whole application to avoid dramatic impacts when the fields’ names change.

Some peopol does such mapping in the “service” layer. But this add some overhead to your application because the “service” layer have to do loop throught all record set to convert it to a “local” array with custom columns names.

On my side, I prefer to make a little mapping mecanism which address the columns names dependencies. Moreover, the method I use brings another bonus: access to data is done thought getter and setter and no more directly (or via __get() and __set()). This way, the code does not rely on columns names and it allows to add some logics to thoses methods if needed.

A (simple) solution

overriding method __call() of Zend_Db_Table_Row

in the following examples, we consider a “User” table with the followings columns: ID, NAME, FORENAME, EMAIL.

 

Going further

Now imagine that the application is near finished and the DBA tell you that the columns of the “User” table has been renamed to : USER_NAME, USER_FORENAME and USER_EMAIL.

User the above solution, you just need to set the “_columnsMapper”  attributs of “Models_User” to the following:

So now for example, “setName” will map the “USER_NAME” attribut.

Final word

As usual, please don’t hesitate to correct me. Also, if something is not clear, I can try to correct it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.