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

Query: Fix cloning #103

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ protected function order(Select $select)

public function __clone()
{
$this->resolver = clone $this->resolver;
$this->resolver = $this->resolver->cloneMe($this);

if ($this->filter !== null) {
$this->filter = clone $this->filter;
Expand Down
27 changes: 27 additions & 0 deletions src/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,31 @@ protected function collectMetaData(Model $subject)

return $definitions;
}

private function setQuery(Query $query): self
{
$this->query = $query;

return $this;
}

public function cloneMe($query)
{
$self = clone $this;
$self->setQuery($query);

return $self;
}

public function __clone()
{
$this->relations = clone $this->relations;
$this->behaviors = clone $this->behaviors;
$this->defaults = clone $this->defaults;
$this->aliases = clone $this->aliases;
$this->selectableColumns = clone $this->selectableColumns;
$this->selectColumns = clone $this->selectColumns;
$this->metaData = clone $this->metaData;
$this->resolvedRelations = clone $this->resolvedRelations;
}
}
46 changes: 46 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ipl\Orm\Exception\InvalidRelationException;
use ipl\Orm\Query;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
use ipl\Tests\Sql\TestCase;

class QueryTest extends TestCase
Expand Down Expand Up @@ -485,4 +486,49 @@ public function testWithoutColumnsDoesNotWorkWithExpressions()
$query->assembleSelect()
);
}

public function testSubQueriesCanBeAdjustedAfterBeingCloned()
{
$query = (new Query())
->setModel(new User())
->setDb(new TestConnection());

$carOneSubQuery = $query->createSubQuery(new Car(), 'user.car')
->columns([new Expression('%s', ['passenger.name'])]);

$carTwoSubQuery = clone $carOneSubQuery;

$carOneSubQuery->filter(Filter::equal('manufacturer', 'wv'));
$carTwoSubQuery->filter(Filter::equal('manufacturer', 'taif'));

list($carOneSelect, $carOneValues) = $carOneSubQuery->dump();
list($carTwoSelect, $carTwoValues) = $carTwoSubQuery->dump();

$query->withColumns([
new Expression($carOneSelect, null, $carOneValues),
new Expression($carTwoSelect, null, $carTwoValues)
]);

$sql = <<<'SQL'
SELECT user.id,
user.username,
user.password,
(SELECT (sub_car_passenger.name) FROM car sub_car
INNER JOIN passenger sub_car_passenger ON sub_car_passenger.car_id = sub_car.id
INNER JOIN car_user sub_car_car_user ON sub_car_car_user.car_id = sub_car.id
INNER JOIN user sub_car_user ON sub_car_user.id = sub_car_car_user.user_id
WHERE (sub_car_user.id = user.id) AND (sub_car.manufacturer = ?)),
(SELECT (sub_car_passenger.name) FROM car sub_car
INNER JOIN passenger sub_car_passenger ON sub_car_passenger.car_id = sub_car.id
INNER JOIN car_user sub_car_car_user ON sub_car_car_user.car_id = sub_car.id
INNER JOIN user sub_car_user ON sub_car_user.id = sub_car_car_user.user_id
WHERE (sub_car_user.id = user.id) AND (sub_car.manufacturer = ?))
FROM user
SQL;

$this->assertSql(
$sql,
$query->assembleSelect()
);
}
}