From 77d5827ef33a53ae47d544ce4cd0a797f7a7a884 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 23 Mar 2023 12:28:17 +0100 Subject: [PATCH] Allow expressions as partition in $fill stage --- .../ODM/MongoDB/Aggregation/Stage/Fill.php | 4 +++- .../Tests/Aggregation/Stage/FillTest.php | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php index 4fc564f64e..39a1fc4b45 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php @@ -86,7 +86,9 @@ public function getExpression(): array $params = (object) []; if ($this->partitionBy) { - $params->partitionBy = $this->partitionBy; + $params->partitionBy = $this->partitionBy instanceof Expr + ? $this->partitionBy->getExpression() + : $this->partitionBy; } if ($this->partitionByFields) { diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FillTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FillTest.php index 7bab5a7ed2..e83b187a62 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FillTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FillTest.php @@ -44,6 +44,30 @@ public function testStage(): void ); } + public function testStageWithExpressionAsPartition(): void + { + $builder = $this->getTestAggregationBuilder(); + $fillStage = new Fill($builder); + $fillStage + ->partitionBy($builder->expr()->year('$field')) + ->sortBy('field1', 1) + ->output() + ->field('foo')->locf(); + + self::assertEquals( + [ + '$fill' => (object) [ + 'partitionBy' => ['$year' => '$field'], + 'sortBy' => (object) ['field1' => 1], + 'output' => (object) [ + 'foo' => ['method' => 'locf'], + ], + ], + ], + $fillStage->getExpression(), + ); + } + public function testStageWithComplexSort(): void { $fillStage = new Fill($this->getTestAggregationBuilder());