Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to reset hydration setting for Aggregation #2353

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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