PHPackages                             rekalogika/doctrine-advanced-group-by - 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. rekalogika/doctrine-advanced-group-by

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

rekalogika/doctrine-advanced-group-by
=====================================

Allows the use of the more complex GROUP BY clauses in Doctrine ORM. These include GROUPING SETS, CUBE, and ROLLUP.

v3.6.0(8mo ago)127.7k—6.3%[3 PRs](https://github.com/rekalogika/doctrine-advanced-group-by/pulls)1MITPHPPHP ^8.2CI passing

Since Feb 2Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/rekalogika/doctrine-advanced-group-by)[ Packagist](https://packagist.org/packages/rekalogika/doctrine-advanced-group-by)[ Docs](https://rekalogika.dev/doctrine-advanced-group-by)[ GitHub Sponsors](https://github.com/priyadi)[ RSS](/packages/rekalogika-doctrine-advanced-group-by/feed)WikiDiscussions orm2 Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (30)Used By (1)

rekalogika/doctrine-advanced-group-by
=====================================

[](#rekalogikadoctrine-advanced-group-by)

Allows the use of the more complex `GROUP BY` clauses in Doctrine ORM. These include `GROUPING SETS`, `CUBE`, and `ROLLUP`.

If you are not familiar with these clauses, you can read more about them in the [PostgreSQL documentation](https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS).

Full documentation is available at [rekalogika.dev/doctrine-advanced-group-by](https://rekalogika.dev/doctrine-advanced-group-by).

Supported Databases
-------------------

[](#supported-databases)

Only PostgreSQL is currently supported. MS SQL Server support is possible in the future. Other Doctrine-supported databases do not have the functionality and cannot be supported, at least not completely.

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

[](#installation)

```
composer require rekalogika/doctrine-advanced-group-by
```

Usage
-----

[](#usage)

```
use Doctrine\ORM\QueryBuilder;
use Rekalogika\DoctrineAdvancedGroupBy\GroupBy;
use Rekalogika\DoctrineAdvancedGroupBy\GroupingSet;
use Rekalogika\DoctrineAdvancedGroupBy\FieldSet;
use Rekalogika\DoctrineAdvancedGroupBy\Field;

/** @var QueryBuilder $queryBuilder */

$queryBuilder
    ->from(SomeEntity::class, 'e')
    ->select('e.a AS a')
    ->addSelect('e.b AS b')
    ->addSelect('e.c AS c')
    ->addSelect('e.d AS d');

$groupBy = new GroupBy(
    new GroupingSet(
        new FieldSet(
            new Field('a'),
            new Field('b'),
        ),
        new FieldSet(
            new Field('c'),
            new Field('d'),
        ),
    ),
);

$query = $queryBuilder->getQuery();
$groupBy->apply($query);
$result = $query->getResult();
```

Flattening
----------

[](#flattening)

It is possible to flatten a `GroupBy` object into another instance of `GroupBy`with a single level of grouping sets.

```
$groupBy1 = new GroupBy(
    new RollUp(
        new Field('a'),
        new Field('b'),
    ),
);

$groupBy2 = new GroupBy(
    new GroupingSet(
        new FieldSet(),
        new FieldSet(
            new Field('a'),
        ),
        new FieldSet(
            new Field('a'),
            new Field('b'),
        ),
    ),
);
```

In the example above, `$groupBy2` is the flattened version of `$groupBy1`. You can transform `$groupBy1` into `$groupBy2` using the `flatten` method.

```
$flattened = $groupBy1->flatten();
```

This is useful if you need to know if the `GroupBy` generates more than 4096 grouping sets, which is the limit of the database. Or, you can use it to split the query into multiple smaller queries.

GROUPING() DQL Function
-----------------------

[](#grouping-dql-function)

If you are using `ROLLUP`, `CUBE`, or `GROUPING SETS`, you also probably need to use the `GROUPING()` function. This package provides a DQL function for the `GROUPING()` function that you can use in a DQL `Query` or `QueryBuilder`.

Registration without framework:

```
use Doctrine\ORM\Configuration;
use Rekalogika\DoctrineAdvancedGroupBy\Function\GroupingFunction;

$configuration = new Configuration();
$configuration->addCustomNumericFunction('GROUPING', GroupingFunction::class);
```

Registration with Symfony:

```
# config/packages/doctrine.yaml
doctrine:
    orm:
        dql:
            numeric_functions:
                GROUPING: Rekalogika\DoctrineAdvancedGroupBy\Function\GroupingFunction
```

Limitations
-----------

[](#limitations)

Works using a custom SQL walker; therefore, it is not possible if you need to use a custom SQL walker for another purpose.

Documentation
-------------

[](#documentation)

[rekalogika.dev/doctrine-advanced-group-by](https://rekalogika.dev/doctrine-advanced-group-by)

License
-------

[](#license)

MIT

Contributing
------------

[](#contributing)

Issues and pull requests should be filed in the GitHub repository [rekalogika/doctrine-advanced-group-by](https://github.com/rekalogika/doctrine-advanced-group-by).

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance67

Regular maintenance activity

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

22

Last Release

267d ago

Major Versions

v2.2.0 → v3.2.02025-06-23

v2.3.0 → v3.4.22025-07-06

v2.4.0 → v3.4.02025-07-06

v2.5.0 → v3.5.02025-08-14

v2.6.0 → v3.6.02025-08-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/13d0eed333cf2f0d552e9a4d0ca1c6a8fc0be0506bbc5e7516f6985b17b86043?d=identicon)[priyadi](/maintainers/priyadi)

---

Top Contributors

[![priyadi](https://avatars.githubusercontent.com/u/1102197?v=4)](https://github.com/priyadi "priyadi (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

ormdoctrinepostgresqlgroupingcubegroup byrollupgrouping sets

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rekalogika-doctrine-advanced-group-by/health.svg)

```
[![Health](https://phpackages.com/badges/rekalogika-doctrine-advanced-group-by/health.svg)](https://phpackages.com/packages/rekalogika-doctrine-advanced-group-by)
```

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[gedmo/doctrine-extensions

Doctrine behavioral extensions

4.1k118.8M366](/packages/gedmo-doctrine-extensions)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[beberlei/doctrineextensions

A set of extensions to Doctrine 2 that add support for additional query functions available in MySQL, Oracle, PostgreSQL and SQLite.

2.1k75.1M146](/packages/beberlei-doctrineextensions)[goodwix/doctrine-json-odm

JSON Object-Document Mapping bundle for Symfony and Doctrine

2226.0k](/packages/goodwix-doctrine-json-odm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
