Skip to content

Commit

Permalink
allow to reset hydration setting for Aggregation (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan authored Aug 4, 2021
1 parent 4979c0f commit 9ca6f5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public function group(): Stage\Group
/**
* Set which class to use when hydrating results as document class instances.
*/
public function hydrate(string $className): self
public function hydrate(?string $className): self
{
$this->hydrationClass = $className;

Expand Down
29 changes: 29 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Documents\CmsComment;
use Documents\GuestServer;
use Documents\Tag;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

use function array_keys;
Expand Down Expand Up @@ -199,6 +200,34 @@ public function testAggregationBuilder()
$this->assertSame(3, $results[0]->numPosts);
}

public function testAggregationBuilderResetHydration(): void
{
$this->insertTestData();

$builder = $this->dm->createAggregationBuilder(BlogPost::class)->hydrate(BlogTagAggregation::class);

$resultCursor = $builder
->hydrate(null)
->unwind('$tags')
->group()
->field('id')
->expression('$tags')
->field('numPosts')
->sum(1)
->sort('numPosts', 'desc')
->getAggregation()
->getIterator();

$this->assertInstanceOf(Iterator::class, $resultCursor);

$results = $resultCursor->toArray();
$this->assertCount(2, $results);
$this->assertIsArray($results[0]);
$this->assertInstanceOf(ObjectId::class, $results[0]['_id']['$id']);
$this->assertSame('Tag', $results[0]['_id']['$ref']);
$this->assertSame(3, $results[0]['numPosts']);
}

public function testGetAggregation()
{
$this->insertTestData();
Expand Down

0 comments on commit 9ca6f5f

Please sign in to comment.