Download the PHP package vladshut/dbal-gateway without Composer
On this page you can find all versions of the php package vladshut/dbal-gateway. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vladshut/dbal-gateway
More information about vladshut/dbal-gateway
Files in vladshut/dbal-gateway
Package dbal-gateway
Short Description Table Gateway for Doctrine DBAL
License MIT
Homepage http://github.com/icomefromthenet/DBALGateway
Informations about the package dbal-gateway
DBALGateway - Table Gateway for Doctrine DBAL.
Doctrine DBAL 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 on top of Doctrine DBAL and is heavily inspired by zf2 Table Gateway.
What are the benefits?
- Using metadata the gateway will convert values, for example DateTime is converted to a stamps and the stamp is converted back to DateTime.
- Events system, e.g pre_select , post_delete , pre_insert... based around the symfony2 event dispatcher.
- Query Logger using Monolog, you would normally write this yourself.
- Builder can map records to entities, you could build an active record on top of this gateway.
- Supply a collection class and it will load them into it.
- Fluid interface for running selects, inserts, updates and deletes.
- Faster than manual CRUD.
Whats are the cons?
- Loose auto-completion in your IDE, for subclasses, only get it for the bases classes.
- Overhead a little more memory and extra method calls.
Install
This component can be installed via composer.
Usage.
There are 3 components to every table.
- Metadata instanceof
DBALGateway\Metadata\Table
. - Subclass of
DBALGateway\Table\AbstractTable
the gateway. - Subclass of
DBALGateway\Query\AbstractQuery
the query class.
There are 2 optional components to every table
- Custom result-set implementation of
Doctrine\Common\Collections\Collection
. - Entity builder implementation of
DBALGateway\Builder\BuilderInterface
.
1. The Metadata
Assume have the following databse table.
You can declare the metadata as follows.
The datatypes (second argument in addColumn) are not mysql types but doctrine types, the mapping can be found under Doctrine\DBAL\Platforms\{MYPLATFORM}
.
2. The Table Gateway.
You will need to declare a subclass of DBALGateway\Metadata\Table
and override the method newQueryObject()
.
You may also include other custom code on this class. But note all methods a marked as protected
be careful with naming.
3. The QueryClass
You will need to subclass DBALGateway\Query\AbstractQuery
which is itself a subclass of Doctrine\DBAL\Query\QueryBuilder
.
Each custom filter should do the following.
- Set a unique named parameter.
- Set the parameter value and fetch the doctrine type from the meta-data in the table gateway.
- Return $this.
4. The Collections class.
When using find()
on the gateway results will be stored in a collection class offerd by doctrine Doctrine\Common\Collections\ArrayCollection
. If the gateway's constructor is passed an instance an alternative that inherits the interface from Doctrine\Common\Collections\Collection
it will clone a copy and use that.
This gateway will clone $collection
on each call to find
. Note: findOne()
does not return collection just entity/array.
5. The Entity Builder
When using find()
or findOne()
each result found in the set will be passed to the builder for conversion into an entity. A builder must implement the interface found at DBALGateway\Builder\BuilderInterface
.
Note: If a collection class is used this new entity will be given to the collection.
Running Queries
Run an INSERT Query.
Run an UPDATE Query.
Run a DELETE Query.
Run a SELECT Query.
There are two methods findOne()
and find()
.
Instance a Gateway?
A Gateway has the following dependecies.
- The table name in the schema.
- The
Doctrine\DBAL\Connection
$connection. - An instance of
Symfony\Component\EventDispatcher\EventDispatcherInterface
. - The meta data for table instance of
DBALGateway\Metadata\Table
. - (optional) a result-set to clone an instance class that implements
Doctrine\Common\Collections\Collection
- (optional) a enity builder an instance class that implements
DBALGateway\Builder\BuilderInterface
Features and Events.
The Gateway emits a number of events.
Event Name | Event Description |
---|---|
pre_initilize | Occurs during object construction. |
post_initilize | Occurs after object construction. |
pre_select | Occurs before a select query is run. |
post_select | Occurs after a select query is run. |
pre_delete | Occurs before delete query is run. |
post_delete | Occurs after delete query is run. |
pre_insert | Occurs before an insert is run. |
post_insert | Occurs after an insert is run. |
pre_update | Occurs before an update is run. |
post_update | Occurs after an update is run. |
For an example see the BufferedQueryLogger.
All versions of dbal-gateway with dependencies
ext-pdo Version *
symfony/event-dispatcher Version ~3.4|~4.0
doctrine/dbal Version ^2.8
doctrine/collections Version ^1.5
monolog/monolog Version ^1.2