PHPackages                             ttskch/doctrine-orm-criteria - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. ttskch/doctrine-orm-criteria

ActiveLibrary[Database &amp; ORM](/categories/database)

ttskch/doctrine-orm-criteria
============================

Doctrine ORM Criteria allows you to separate any complex "search condition" as a Criteria with a specialized API for QueryBuilder of doctrine/orm.

1.2.2(6mo ago)23.9k↓29.6%MITPHPPHP ^8.1CI passing

Since Aug 22Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/ttskch/doctrine-orm-criteria)[ Packagist](https://packagist.org/packages/ttskch/doctrine-orm-criteria)[ RSS](/packages/ttskch-doctrine-orm-criteria/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

Doctrine ORM Criteria
=====================

[](#doctrine-orm-criteria)

[![](https://github.com/ttskch/doctrine-orm-criteria/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/ttskch/doctrine-orm-criteria/actions/workflows/ci.yaml?query=branch:main)[![codecov](https://camo.githubusercontent.com/6711ee425aab79dbab49903f7ad463d517cd4ec9d716ca389c1aa069fe9f5de2/68747470733a2f2f636f6465636f762e696f2f67682f7474736b63682f646f637472696e652d6f726d2d63726974657269612f67726170682f62616467652e7376673f746f6b656e3d67753147447068424867)](https://codecov.io/gh/ttskch/doctrine-orm-criteria)[![Latest Stable Version](https://camo.githubusercontent.com/0aa3d030d03aac921a8f820c6e86f4d63ca626bd3d388ce8d2d9930a20f52265/68747470733a2f2f706f7365722e707567782e6f72672f7474736b63682f646f637472696e652d6f726d2d63726974657269612f76)](https://packagist.org/packages/ttskch/doctrine-orm-criteria)[![Total Downloads](https://camo.githubusercontent.com/8fb09ec221eb5492429480acbebbb8fb92305156340b9e67cf52b39c8b43cadf/68747470733a2f2f706f7365722e707567782e6f72672f7474736b63682f646f637472696e652d6f726d2d63726974657269612f646f776e6c6f616473)](https://packagist.org/packages/ttskch/doctrine-orm-criteria/stats)

Motivation
----------

[](#motivation)

[`QueryBuilder`](https://www.doctrine-project.org/projects/doctrine-orm/en/2.20/reference/query-builder.html) of [doctrine/orm](https://www.doctrine-project.org/projects/doctrine-orm/en/2.20/index.html) has a method called [`addCriteria()`](https://www.doctrine-project.org/projects/doctrine-orm/en/2.20/reference/query-builder.html#adding-a-criteria-to-a-query) that allows you to build queries by combining [`Criteria`](https://www.doctrine-project.org/projects/doctrine-orm/en/2.20/reference/working-with-associations.html#filtering-collections) of [doctrine/collections](https://www.doctrine-project.org/projects/doctrine-collections/en/stable/index.html). This allows you to separate the concerns of "search conditions" into a `Criteria`, improving the maintainability of your codebase.

However, `Criteria` of doctrine/collections only has a very limited matching language because it is designed to work both on the SQL and the PHP collection level, and therefore cannot be used to build complex queries.

Rejoice! **Doctrine ORM Criteria** allows you to separate any complex "search condition" as a `Criteria` with a specialized API for `QueryBuilder` of doctrine/orm just like below.

```
$qb = (new CriteriaAwareness($fooRepository->createQueryBuilder('f')))
    ->addCriteria(new IsPublic(), 'f')
    ->addCriteria(new IsAccessibleBy($user), 'f')
    ->addCriteria(new CategoryIs($category), 'f')
    ->addCriteria(new OrderByRandom(), 'f')
    ->getQueryBuilder()
;
$foos = $qb->getQuery()->getResult();

// Or, using the Repository integration:

$foos = $fooRepository->findByCriteria([
    new IsPublic(),
    new IsAccessibleBy($user),
    new CategoryIs($category),
    new OrderByRandom(),
]);
```

```
final readonly class IsPublic implements CriteriaInterface
{
    public ?\DateTimeInterface $at;

    public function __construct(?\DateTimeInterface $at = null)
    {
        $this->at = $at ?? new \DateTimeImmutable();
    }

    public function apply(QueryBuilder $qb, string $alias): void
    {
        $qb
            ->andWhere("$alias.state = :state")
            ->andWhere($qb->expr()->andX(
                $qb->expr()->orX(
                    "$alias.openedAt IS NULL",
                    "$alias.openedAt  :at",
                ),
            ))
            ->setParameter('state', Foo::STATE_PUBLIC)
            ->setParameter('at', $this->at)
        ;
    }
}
```

Requirements
------------

[](#requirements)

- PHP: ^8.1
- Doctrine ORM: ^2.8|^3.0

Installation
------------

[](#installation)

```
$ composer require ttskch/doctrine-orm-criteria
```

Usage
-----

[](#usage)

### Basic

[](#basic)

You can create your own `Criteria` by implementing [`CriteriaInterface`](src/Criteria/CriteriaInterface.php) and adding it to [`CriteriaAwareness`](src/CriteriaAwareness.php) to build queries.

```
use App\Repository\Criteria\Foo\IsPublic;
use Ttskch\DoctrineOrmCriteria\CriteriaAwareness;

$qb = (new CriteriaAwareness($fooRepository->createQueryBuilder('f')))
    ->addCriteria(new IsPublic(), 'f')
    ->getQueryBuilder()
;
```

```
