From 9ca6f5f9b0198da8ee930796255b4714c75db07d Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Wed, 4 Aug 2021 21:20:42 +0800 Subject: [PATCH] allow to reset hydration setting for Aggregation (#2353) --- .../ODM/MongoDB/Aggregation/Builder.php | 2 +- .../MongoDB/Tests/Aggregation/BuilderTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php index 39c27ba740..d4cb88400a 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php @@ -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; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php index 1d52a40580..80edd42d3c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php @@ -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; @@ -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();