Skip to content
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
41 changes: 6 additions & 35 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,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;
Expand Down Expand Up @@ -176,7 +176,7 @@
}

if (empty($this->getSelect()) && !empty($this->getJoins())) {
[, $alias] = $this->getTableNameAndAlias();
[, $alias] = TableNameAndAliasResolver::resolve($this);

$this->select(["$alias.*"]);
}
Expand Down Expand Up @@ -557,33 +557,6 @@
: $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.
*
Expand All @@ -607,8 +580,7 @@
}

$via = $child->getVia();
/** @var ActiveQuery $child */
$child->via = null;
$child->resetVia();

if ($via instanceof ActiveQueryInterface) {
// via table
Expand All @@ -626,9 +598,8 @@
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, '{{')) {
Expand Down Expand Up @@ -723,7 +694,7 @@
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();
Expand Down Expand Up @@ -810,7 +781,7 @@
return clone $this->model;
}

public function batch(int $batchSize = 100): BatchQueryResultInterface

Check warning on line 784 in src/ActiveQuery.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ { return clone $this->model; } - public function batch(int $batchSize = 100): BatchQueryResultInterface + public function batch(int $batchSize = 101): BatchQueryResultInterface { /** * @var Closure(non-empty-array<array>):non-empty-array<object> $callback
{
/**
* @var Closure(non-empty-array<array>):non-empty-array<object> $callback
Expand Down
2 changes: 2 additions & 0 deletions src/ActiveQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions src/Internal/TableNameAndAliasResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Internal;

use LogicException;
use Yiisoft\ActiveRecord\ActiveQueryInterface;
use Yiisoft\Db\Expression\ExpressionInterface;

use function is_string;

/**
* @internal
*/
final class TableNameAndAliasResolver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to reuse it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only inside the package

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in this place or somewhere else as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is now in PR

{
/**
* Returns the table name and the table alias.
*
* @psalm-return list{ExpressionInterface|string, string}
*/
public static function resolve(ActiveQueryInterface $query): array
{
$from = $query->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];
}
}
13 changes: 0 additions & 13 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down