Tech blog


ORM dos and don'ts in PHP apps.

It's been a long time since I was first introduced to the concepts of Object-Relational Mapping (ORM). First time I took a deeper look at this subject in ~2004. Since then, my mind has changed, and my perception of ORM has changed either. There was time when I thought that SQL can be practically eliminated from the application, and design patterns like Unit-of-Work echoed with warmth and joy inside my head. Well, this is no more. For the last several years my feelings about ORM stay mostly unchanged, and as such, I want now to summarize my opinion in this post.

The following is mostly related to a typical PHP application that stores data in RDBMS. Such apps usually have a short-living request-response cycle:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-     ☺   User     ↑     ↓     ↑    HTTP Request     ↑     ↓     ↑    Web-server     ↑     ↓     ↑    PHP     ↑     ↓     ↑    Process particular request (typically involves talking to a database)     ↑     ↓                                                                     Return result to the user -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

All of this usually takes 10 - 500 ms. The developer usually does not deal with multithreaded environments, in contrast to, f. e. Java. It does not mean there is no concurrency, of course, but that's a different issue: at least you don't have to worry that an object you're working with is being modified at the very same time in a neighboring thread.

The awareness of the short cycle of request-response has largely formed my attitude to ORM in PHP applications. In short: I like ORM but I like it simple. Below are a few points that I have set for myself.



TECH: HandlerSocket protocol explained

So, you come to HandlerSocket. Nice. It is a damn fast voodoo. And the protocol it talks to its clients is really simple. And because in most cases you just use some connector library, you don't really care about the details of the protocol. Yet if you're curious, you find this page. And, if you spend a few hours there, you'll figure out how it goes. Or... you may spend just 15 minutes reading this article.

I will assume that you already have a working MySQL/MariaDB/Percona server installation with a handlersocket plugin. I used MariaDB, and it ships with a built-in handlersocket, you will just have to enable it and supply a little bit of configuration.

Check out my PHP Extension for HandlerSocket on GitHub: !!! and...


Order print