gbprod/doctrine-specification

此包已被弃用,不再维护。未建议替代包。

此库允许您使用规范模式编写 Doctrine ORM 查询

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

此库允许您使用 规范模式 编写 Doctrine ORM 查询。

使用方法

您可以使用 gbprod/specification 库来编写规范。

创建 doctrine 规范过滤器

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"),
            )
        ;
    }
}

配置

$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()
);

使用它

$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();

要求

  • PHP 7.0+

安装

使用 Composer

composer require gbprod/doctrine-specification