From b3b6f81530fbb736ef53c9b3bee819d08b85bc10 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 31 Oct 2025 09:44:57 +0300 Subject: [PATCH 1/2] Extract `TableNameAndAliasResolver` to internal class + Add `ActiveQuery::resetVia()` --- src/ActiveQuery.php | 40 +++---------------- src/ActiveQueryInterface.php | 2 + src/ActiveRelationTrait.php | 6 +++ src/Internal/TableNameAndAliasResolver.php | 45 ++++++++++++++++++++++ tests/ActiveQueryTest.php | 13 ------- 5 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 src/Internal/TableNameAndAliasResolver.php diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 21b008d84..2ea0cce2e 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -11,6 +11,7 @@ use Yiisoft\ActiveRecord\Internal\ArArrayHelper; use Yiisoft\ActiveRecord\Internal\JunctionRowsFinder; use Yiisoft\ActiveRecord\Internal\ModelRelationFilter; +use Yiisoft\ActiveRecord\Internal\TableNameAndAliasResolver; use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; @@ -176,7 +177,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface } if (empty($this->getSelect()) && !empty($this->getJoins())) { - [, $alias] = $this->getTableNameAndAlias(); + [, $alias] = TableNameAndAliasResolver::resolve($this); $this->select(["$alias.*"]); } @@ -557,33 +558,6 @@ private function getJoinType(array|string $joinType, string $name): string : $joinType; } - /** - * Returns the table name and the table alias. - * - * @psalm-return list{ExpressionInterface|string, string} - */ - private function getTableNameAndAlias(): array - { - if (empty($this->from)) { - $tableName = $this->getPrimaryTableName(); - } else { - $alias = array_key_first($this->from); - $tableName = $this->from[$alias]; - if (is_string($alias)) { - return [$tableName, $alias]; - } - if ($tableName instanceof ExpressionInterface) { - throw new LogicException('Alias must be set for a table specified by an expression.'); - } - } - - $alias = preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches) - ? $matches[2] - : $tableName; - - return [$tableName, $alias]; - } - /** * Joins a parent query with a child query. * @@ -607,8 +581,7 @@ private function joinWithRelation(ActiveQueryInterface $parent, ActiveQueryInter } $via = $child->getVia(); - /** @var ActiveQuery $child */ - $child->via = null; + $child->resetVia(); if ($via instanceof ActiveQueryInterface) { // via table @@ -626,9 +599,8 @@ private function joinWithRelation(ActiveQueryInterface $parent, ActiveQueryInter return; } - /** @var ActiveQuery $parent */ - [$parentTable, $parentAlias] = $parent->getTableNameAndAlias(); - [$childTable, $childAlias] = $child->getTableNameAndAlias(); + [, $parentAlias] = TableNameAndAliasResolver::resolve($parent); + [$childTable, $childAlias] = TableNameAndAliasResolver::resolve($child); if (!empty($child->getLink())) { if (!str_contains($parentAlias, '{{')) { @@ -723,7 +695,7 @@ public function viaTable(string $tableName, array $link, callable|null $callable public function alias(string $alias): static { if (count($this->from) < 2) { - [$tableName] = $this->getTableNameAndAlias(); + [$tableName] = TableNameAndAliasResolver::resolve($this); $this->from = [$alias => $tableName]; } else { $tableName = $this->getPrimaryTableName(); diff --git a/src/ActiveQueryInterface.php b/src/ActiveQueryInterface.php index 57022842d..f6bba6f98 100644 --- a/src/ActiveQueryInterface.php +++ b/src/ActiveQueryInterface.php @@ -112,6 +112,8 @@ public function getWith(): array; */ public function via(string $relationName, callable|null $callable = null): static; + public function resetVia(): static; + /** * @return array|ExpressionInterface|string|null the join condition to be used when this query is used in a relational context. * diff --git a/src/ActiveRelationTrait.php b/src/ActiveRelationTrait.php index 0b2444ee9..b0468cf42 100644 --- a/src/ActiveRelationTrait.php +++ b/src/ActiveRelationTrait.php @@ -107,6 +107,12 @@ public function via(string $relationName, callable|null $callable = null): stati return $this; } + public function resetVia(): static + { + $this->via = null; + return $this; + } + public function inverseOf(string $relationName): static { $this->inverseOf = $relationName; diff --git a/src/Internal/TableNameAndAliasResolver.php b/src/Internal/TableNameAndAliasResolver.php new file mode 100644 index 000000000..e92967d2a --- /dev/null +++ b/src/Internal/TableNameAndAliasResolver.php @@ -0,0 +1,45 @@ +getFrom(); + if (empty($from)) { + $tableName = $query->getModel()->tableName(); + } else { + $alias = array_key_first($from); + $tableName = $from[$alias]; + if (is_string($alias)) { + return [$tableName, $alias]; + } + if ($tableName instanceof ExpressionInterface) { + throw new LogicException('Alias must be set for a table specified by an expression.'); + } + } + + $alias = preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches) + ? $matches[2] + : $tableName; + + return [$tableName, $alias]; + } +} diff --git a/tests/ActiveQueryTest.php b/tests/ActiveQueryTest.php index 049181022..f13d3fe1c 100644 --- a/tests/ActiveQueryTest.php +++ b/tests/ActiveQueryTest.php @@ -142,19 +142,6 @@ public function testBuildJoinWithRemoveDuplicateJoinByTableName(): void ], $query->getJoins()); } - public function testGetQueryTableNameFromNotSet(): void - { - $query = Customer::query(); - $this->assertEquals(['customer', 'customer'], Assert::invokeMethod($query, 'getTableNameAndAlias')); - } - - public function testGetQueryTableNameFromSet(): void - { - $query = Customer::query(); - $query->from(['alias' => 'customer']); - $this->assertEquals(['customer', 'alias'], Assert::invokeMethod($query, 'getTableNameAndAlias')); - } - public function testOnCondition(): void { $on = ['active' => true]; From 53dd3b0407356df8dee4775bbe06c13bd4c95b62 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 31 Oct 2025 06:45:17 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/ActiveQuery.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 2ea0cce2e..4fab10539 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -38,7 +38,6 @@ use function in_array; use function is_array; use function is_int; -use function is_string; use function preg_match; use function reset; use function serialize;