Skip to content

Commit

Permalink
Add $sortByCount aggregation pipeline stage
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Oct 9, 2017
1 parent 7f3c958 commit f5bc642
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ public function replaceRoot($expression = null)
return $this->addStage(new Stage\ReplaceRoot($this, $this->dm, $this->class, $expression));
}

/**
* @return Stage\SortByCount
*/
public function sortByCount($expression)
{
return $this->addStage(new Stage\SortByCount($this, $expression, $this->dm, $this->class));
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 18 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/SortByCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

use Doctrine\MongoDB\Aggregation\Builder;
use Doctrine\MongoDB\Aggregation\Stage as BaseStage;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

class SortByCount extends BaseStage\SortByCount
{
public function __construct(Builder $builder, $fieldName, DocumentManager $documentManager, ClassMetadata $class)
{
$documentPersister = $documentManager->getUnitOfWork()->getDocumentPersister($class->name);

parent::__construct($builder, '$' . $documentPersister->prepareFieldName(substr($fieldName, 1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Aggregation\Stage;

use Documents\CmsComment;

class SortByCountTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function testFieldNameConversion()
{
$builder = $this->dm->createAggregationBuilder(CmsComment::class);
$builder->sortByCount('$authorIp');

$this->assertEquals(
[['$sortByCount' => '$ip']],
$builder->getPipeline()
);
}
}

0 comments on commit f5bc642

Please sign in to comment.