Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How does an implementation of Table Data Gateway differs from Active Record?

0 like 0 dislike


Put simply about Table Data Gateway (TDG) implementation: you create separate TDG class that contains SQL for CRUD operations with concrete table. So, your models communicate not directly with a datasource (for example database) but via those abstracted ones - TDG classes. So, it's just an approach for making another level of abstraction and it's just a wrapper for communicating with database - get and modify the data. IMHO TDG classes should not contain members, only methods. Here's a good schema that visualizes the use TDG pattern. When using TDG approach, SQL should be moved from model classes to datasource (TDG) classes. And all data that I retrieve from DB through TDG classes are stored in my model members.

Now, what about active record implementation? If I would merge data access and my model class into one model class then would I implement active record? I was unable to find a clear distinction or how those patterns look in PHP and differ from each other.

Often, I'm having one singleton database class and then separate model class for each database table. Each model class has CRUD + several custom (count, avg and etc.) operations. Some classes have members for persisting results from CRUD or custom operations - thats's done upon the need. Could this approach be identified as active record? Also, if I would move SQL from my model classes to TDG classes would this be Table Data Gateway?

asked Jan 1, 2012 by anonymous  

1 Answer

0 like 0 dislike



Table Data Gateway: An object that acts as a Gateway (466) to a database table. One instance handles all the rows in the table.

enter image description here

Active Record: An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.

enter image description here

The obvious main difference is that TDGs wrap access to a table and only returns row data, while ARs wrap access to a row in the table and adds business logic to it.

Unless you have very low impedance mismatch, having a TDG is preferred because AR with AR your business/domain objects follow the structure in the database, e.g. they are structures like rows. But usually, domain objects are not modeled that way.

answered Jan 1, 2012 by anonymous