Skip to content

Add adapter to support Mongo Aggregation Builder #277

@webdevilopers

Description

@webdevilopers

Currently the DoctrineODMMongoDBAdapter only supports the Query Builder:

But there is also the Aggregation Builder:

Adding an additional adapter based on the existing one is actually easy:

use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Pagerfanta\Adapter\AdapterInterface;

class DoctrineODMMongoDBAggregationAdapter implements AdapterInterface
{
    private $aggregationBuilder;

    /**
     * Constructor.
     *
     * @param Builder $aggregationBuilder A DoctrineMongo aggregation builder.
     */
    public function __construct(Builder $aggregationBuilder)
    {
        $this->aggregationBuilder = $aggregationBuilder;
    }

    /**
     * Returns the query builder.
     *
     * @return Builder The query builder.
     */
    public function getAggregationBuilder()
    {
        return $this->aggregationBuilder;
    }

    /**
     * {@inheritdoc}
     */
    public function getNbResults()
    {
        return $this->aggregationBuilder->execute()->count();
    }

    /**
     * {@inheritdoc}
     */
    public function getSlice($offset, $length)
    {
        return $this->aggregationBuilder
            ->limit($length)
            ->skip($offset)
            ->execute();
    }
}

Unfortunately none of the two builders implement a common Interface. But there is another reason why using a single adapter for both use cases could be problematic!

The aggregation builder uses a pipeline. Each time a so called "stage" e.g. limit oder sort is added the return value of the assigned variable changes.

$qb = $this
    ->createAggregationBuilder();
    ->sort(['leavingDate' => 1])
    ->limit($query->limit);

dump($qb); // Instance of Limit extends Stage

$qb = $this
    ->createAggregationBuilder();
$qb
    ->sort(['leavingDate' => 1])
    ->limit($query->limit);

dump($qb); // Instance of Builder

As soon as you pass a Stage instance you can not retrieve the original builder:

Also there are plans for ODM version 2 which may change this behaviour:

Maybe @alcaeus or @malarzm could comment on this too? Thanks in advance!

Possibly related issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions