An implementation of the Specification Pattern to create reusable DQL queries for Doctrine ORM.
The specification pattern is an alternative approch to creating database queries using Doctrine ORM. The pattern decouples and hides the ORM's query builder implementation details from client code and reduces the need for numerious and repetative
findBy* repository methods. Each specfification is a self contained and reusable object that can be chained with other specifications to easily produce complex DQL queries with a fluid, testable and easy to understand API.
Installtion via Composer package manager. Simply add the package name to your
php composer.phar require alex-patterson-webdev/arp-orm-specification : 0.9
The standard specifications, which closley resemble the QueryBuilder's expression classes classes, represent the smallest possible components needed to creating a DQL query.
There are two main types of specifications
Is used to compare one value with another.
Two or more specifications combined together to form one single specification.
Although all specifications are able to be created in line, the module also ships with a specification factory,
ArpOrmSpecification\Service\OrmSpecificationFactory, which provides a simple API to create new specifications.
$sf = new \ArpOrmSpecification\Service\OrmSpecificationFactory(); $spec1 = $sf->isEqual('foo', 'bar'); $spec2 = new \ArpOrmSpecification\Entity\Spec\IsEqual('foo', 'bar');
$spec2 are both instances of the comparion specification
IsEqual. When filtered both specifications produce identical DQL
foo = 'bar'.
The factory reduces the boilerplate code needed when composing different specifications together.
$sf = new \ArpOrmSpecification\Service\OrmSpecificationFactory(); $spec = $sf->andX( $sf->orX( $sf->isNotNull('foo'), $sf->isGreaterThanOrEqualTo('bar', 123) ), $this->isEqual('name', 'bob') );