gbprod / doctrine-specification
This library allows you to write Doctrine ORM queries using the specification pattern
Installs: 3 035
Dependents: 1
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
- doctrine/orm: ^2.5
- gbprod/specification: ^2.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0
This package is auto-updated.
Last update: 2021-01-07 09:20:31 UTC
README
This library allows you to write Doctrine ORM queries using the specification pattern.
Usage
You can write specifications using gbprod/specification library.
Creates a doctrine specification filter
namespace GBProd\Acme\Doctrine\SpecificationFactory; use GBProd\DoctrineSpecification\QueryFactory\Factory; use GBProd\Specification\Specification; use Doctrine\ORM\QueryBuilder; class IsAvailableFactory implements Factory { public function create(Specification $spec, QueryFactory $qb) { return $qb->expr() ->andx( $qb->expr()->eq('available', "0"), $qb->expr()->gt('limitDate', "2016-03-05 00:00:00"), ) ; } }
Configure
$registry = new GBProd\DoctrineSpecification\Registry(); $handler = new GBProd\DoctrineSpecification\Handler( $registry, $this->em->createQueryBuilder() ); $handler->registerFactory( IsAvailable::class, new IsAvailableFactory() ); $handler->registerFactory( StockGreaterThan::class, new StockGreaterThanFactory() );
Use it
$availableWithLowStock = (new IsAvailable()) ->andX((new StockGreaterThan(4))->not()) ; $qb = $this->em ->getRepository('GBProd\Acme\CoreDomain\Product\Product') ->createQueryBuilder('p') ; $qb->where( $handler->handle($availableWithLowStock) ); return $qb->getQuery()->getResult();
Requirements
- PHP 7.0+
Installation
Using composer
composer require gbprod/doctrine-specification