gbprod/doctrine-specification

This package is abandoned and no longer maintained. No replacement package was suggested.

This library allows you to write Doctrine ORM queries using the specification pattern

v2.1.0 2018-02-08 11:07 UTC

This package is auto-updated.

Last update: 2021-01-07 09:20:31 UTC


README

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

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