diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 1b05e7427..9306447d4 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -870,7 +870,7 @@ private function createInstance(): static ->having($this->having) ->setUnions($this->union) ->params($this->params) - ->withQueries($this->withQueries); + ->withQueries(...$this->withQueries); } private function populateOne(array $row): ActiveRecordInterface|array diff --git a/src/ActiveQueryInterface.php b/src/ActiveQueryInterface.php index 51f52b0a0..dcffab4de 100644 --- a/src/ActiveQueryInterface.php +++ b/src/ActiveQueryInterface.php @@ -95,6 +95,11 @@ public function asArray(bool|null $value = true): static; */ public function with(array|string ...$with): static; + /** + * @return array A list of relations that this query should be performed with. + */ + public function getWith(): array; + /** * Specifies the relation associated with the junction table for use in a relational query. * diff --git a/src/ActiveQueryTrait.php b/src/ActiveQueryTrait.php index 0dddb5f56..7b46660cb 100644 --- a/src/ActiveQueryTrait.php +++ b/src/ActiveQueryTrait.php @@ -20,6 +20,7 @@ trait ActiveQueryTrait { private bool|null $asArray = null; + private array $with = []; /** * Sets the {@see asArray} property. @@ -146,7 +147,7 @@ protected function createModels(array $rows): array * @psalm-param non-empty-list $models * @psalm-param-out non-empty-list $models */ - public function findWith(array $with, array &$models): void + private function findWith(array $with, array &$models): void { $primaryModel = reset($models); diff --git a/tests/ActiveQueryTest.php b/tests/ActiveQueryTest.php index 00f98d064..7187a86af 100644 --- a/tests/ActiveQueryTest.php +++ b/tests/ActiveQueryTest.php @@ -96,6 +96,20 @@ public function testGetJoinWith(): void $this->assertEquals([[['profile'], true, 'LEFT JOIN']], $query->getJoinWith()); } + public function testGetWith(): void + { + $query = Customer::query(); + + $this->assertSame([], $query->getWith()); + + $query->with('orders'); + $this->assertSame(['orders'], $query->getWith()); + + $query = Customer::query(); + $query->with('orders', 'profile'); + $this->assertSame(['orders', 'profile'], $query->getWith()); + } + public function testInnerJoinWith(): void { $query = Customer::query();