PHPackages                             vladshut/dbal-gateway - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. vladshut/dbal-gateway

ActiveLibrary[Database &amp; ORM](/categories/database)

vladshut/dbal-gateway
=====================

Table Gateway for Doctrine DBAL

v1.2.0(7y ago)0131MITPHPPHP ^7.1

Since Oct 6Pushed 7y agoCompare

[ Source](https://github.com/vladshut/DbalGateway)[ Packagist](https://packagist.org/packages/vladshut/dbal-gateway)[ Docs](http://github.com/icomefromthenet/DBALGateway)[ RSS](/packages/vladshut-dbal-gateway/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (1)Dependencies (6)Versions (4)Used By (1)

DBALGateway - Table Gateway for Doctrine DBAL.
==============================================

[](#dbalgateway---table-gateway-for-doctrine-dbal)

[![Build Status](https://camo.githubusercontent.com/b95f82a03aec861b033f08790ab4f168427f3338ff7efa820666e09ff9852536/68747470733a2f2f7472617669732d63692e6f72672f69636f6d6566726f6d7468656e65742f4442414c476174657761792e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/icomefromthenet/DBALGateway)

[Doctrine DBAL](http://www.doctrine-project.org/projects/dbal.html) is a fantastic extension to PDO but writing CRUD code for simple 1 to 1 mappings still represents a time sink. This component implements [Table Gateway](http://martinfowler.com/eaaCatalog/tableDataGateway.html) on top of Doctrine DBAL and is heavily inspired by zf2 Table Gateway.

What are the benefits?
----------------------

[](#what-are-the-benefits)

1. Using metadata the gateway will convert values, for example DateTime is converted to a stamps and the stamp is converted back to DateTime.
2. Events system, e.g pre\_select , post\_delete , pre\_insert... based around the symfony2 event dispatcher.
3. Query Logger using Monolog, you would normally write this yourself.
4. Builder can map records to entities, you could build an active record on top of this gateway.
5. Supply a collection class and it will load them into it.
6. Fluid interface for running selects, inserts, updates and deletes.
7. Faster than manual CRUD.

Whats are the cons?
-------------------

[](#whats-are-the-cons)

1. Loose auto-completion in your IDE, for subclasses, only get it for the bases classes.
2. Overhead a little more memory and extra method calls.

Install
-------

[](#install)

This component can be installed via composer.

```
{
    "require" : {
        "icomefromthenet/dbal-gateway" : "dev-master",
    }

}
```

Usage.
------

[](#usage)

There are 3 components to every table.

1. Metadata instanceof `DBALGateway\Metadata\Table`.
2. Subclass of `DBALGateway\Table\AbstractTable` the gateway.
3. Subclass of `DBALGateway\Query\AbstractQuery` the query class.

There are 2 optional components to every table

4. Custom result-set implementation of `Doctrine\Common\Collections\Collection`.
5. Entity builder implementation of `DBALGateway\Builder\BuilderInterface`.

### 1. The Metadata

[](#1-the-metadata)

Assume have the following databse table.

```
delimiter $$

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `first_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `dte_created` datetime NOT NULL,
  `dte_updated` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$
```

You can declare the metadata as follows.

```
