Are TableGateways Worth it in Zend Framework 2?

Are TableGateways too hard to implement in Zend Framework 2? Too hard to justify the effort? That’s what I was asked recently. So I’ve written this post to show why they’re not, and how they bring great flexibility, when implemented correctly.


Are TableGateways too hard to implement in Zend Framework 2? Are they too hard to justify the effort? That’s what I was asked recently during a Twitter conversation. For me, they’re not. For me, they’re well worth the effort. So I’ve written this post to show why they’re not, and how they bring great flexibility, when implemented correctly.

Here’s how it all started.

@zwelibanzi360: TableGateway pattern is tedious. I just pass an adapter to a model class and use Db\Select #zf2 @maltblue, your thoughts…

In a follow up tweet he wrote:

3 things 4 TableGateway:

  1. TableGateway class
  2. class implementing exchangeArray
  3. Configure TableGateway via ServiceManager

In the conversation which ensued, zwelibanzi said he felt that, for the size of his project, it was overkill yet was curious nonetheless about what others, myself and Samsonasik thought. So in this follow up to a recent TableGateway post here on Malt Blue, I’ll set out why they can be a good solution, as well as why not.

TableGateways, as the name implies, stand between a table and your application. To quote Martin Fowler:

I find that Table Data Gateway is probably the simplest database interface pattern to use as it maps so nicely onto a database table or record type. It also makes a natural point to encapsulate the precise access logic of the data source.

Patterns of Enterprise Application Architecture - Martin Fowler, pages 2793, 2794.

I’m sure all of us, who’ve been around PHP long enough, remember a time when a PHP application was simply a collection of files, interspersed with a mixture of PHP and HTML.

Our applications started out modestly, consisting of only a few .php or .inc files. Naturally, you could argue there was no harm in having direct database calls in the files. There’s not too many, they’re easy to maintain. Yeah, ok, granted.

But then the complexity grew, and more calls were added in more files. Potentially you started to see duplicate, or near duplicate, code appearing. Now it begins to get harder to maintain.

You may have been astute, or organised enough, to collate all of these calls into a single include file. You may have built a class which encapsulated them even. If you had, you’d have had the basis of a TableGateway. Through this central point, if you enforced its usage, without knowing the pattern, you were already using it.

If however, this collation was avoided, or missed, the code would, in time, become a tangled, near unworkable, mess. If you disagree, try doing the following with your application:

  • Migrate to another vendor
  • Support multiple vendors (PostgreSQL, Oracle, MSSQL Server, MySQL, SQLite)
  • Have reads from one database and writes to another, transparently
  • Enforce DRY
  • Stay on top of potential bugs

Implementing Them in Zend Framework

As mentioned earlier, there are 3 steps for implementing them in Zend Framework. Let’s look at an example of how to do so, in the following 3 gists.

In the gists, we have 2 classes, one a TableGateway, the other a Model. The TableGateway class contains table-aware logic, such as queries to fetch all and fetch by specific criteria. The model class is table agnostic. It only knows about the properties that it needs data for.

Then, we have the module configuration, which binds the two together. Firstly, we have a TableGateway configuration, so we know which table we’re binding to the TableGateway. Then we have the table configuration. I’ve taken this configuration further by using a HydratingResultset object. What this does, is to also implement the RowGateway pattern.

What it does is use our model class for every record in the resultset. This makes it that much simpler to work with the records retrieved. You’ll notice a very simple mapper too. It’s not the greatest implementation in the world - one of the first ones that I wrote and I’m phasing out.

But, what it does is allow for automatic persistence of record information in our model class, mapping from table columns to model properties. As you can see, that is a bit of code, just to interact with a table. You need to be discerning in your needs assessment, both now and into the future.

Ask yourself the following two questions:

  • What if the amount of tables, and views, grows. How many classes will you need to build and maintain?
  • Is your application really bit enough to warrant this level of effort?

But before you get too concerned, I’d suggest that past a point you’d either have a more sophisticated solution, which is beyond the scope of this post.

Some More Advanced Cases

If you do choose this path though, here’s two potential advantages to think about:

  • Access views instead of tables, and base that access on user privileges. Now you reuse the same objects, and the same calls, but reflect the logged in user. This helps keep code to a minimum.
  • Have a common set of base classes which store a lot of the common functionality, such as findById, creating, updating and deleting records. Then build on them in other classes
  • Reuse the common base class by passing in different TableGateway configurations, thereby minimising code creation

These are just a few examples, I’m sure you can think of more. But the show how taking this approach opens up a raft of potential. For me, in all but the most trivial of cases, a TableGateway is a good choice to make, based on the flexibility it brings. I hope that through this short post, you can see why implementing a TableGateway can make development more efficient.

Yes, for small projects it can be, or seem like, overkill. But as project complexity grows, as it invariably does, you won’t miss the time not spent maintaining your application.

What other ways could you use a TableGateway? Share your thoughts in the comment.


You might also be interested in these tutorials too...

Wed, Jan 29, 2014

Easy Setter Injection in Zend Framework 2

Want to learn how to have initialized objects with minimal code and nearly no hands-on configuration in Zend Framework 2? Come learn about setter injection.

Tue, Jul 2, 2013

Zend\Db\Sql\Select - The Basics (Columns, Limit & Order)

In part 3 of this series on \Zend\Db\Sql\Select in Zend Framework 2 We cover the constructor, columns function, aliases and expressions, finishing up with limit and order functions. Come wind up the series in style.

Thu, Jun 27, 2013

\Zend\Db\Sql - Creating Joins and Unions with Ease

In part two of the \Zend\Db\Sql\Select series, we create inner, outer, left and right joins as well as union queries with ease. Come learn more about building queries simply, effectively and easily in your every day applications with Zend Framework 2.

Thu, Jun 20, 2013

\Zend\Db\Sql - Build SQL Where Clauses Easily and Efficiently

In this tutorial we’re working through the basics of \Zend\Db\Sql\Where, showing how to build SQL Where clauses for database queries. We’ll show how to create different predicates from simple to nested queries using of and, or, like, between and in conditions.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus