Skip to content

Commit

Permalink
Revert "Standardize withCount() & withExists() eager loading aggr…
Browse files Browse the repository at this point in the history
…egates. (#41914)" (#41943)

This reverts commit f5c50ff.
  • Loading branch information
taylorotwell authored Apr 12, 2022
1 parent 7dad785 commit 113b04a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 64 deletions.
20 changes: 4 additions & 16 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,7 @@ public function withAggregate($relations, $column, $function = null)
*/
public function withCount($relations)
{
$relations = is_array($relations) ? $relations : func_get_args();

if (count($relations) === 2 && $relations[1] instanceof Closure) {
$relations = [$relations[0] => $relations[1]];
}

return $this->withAggregate($relations, '*', 'count');
return $this->withAggregate(is_array($relations) ? $relations : func_get_args(), '*', 'count');
}

/**
Expand Down Expand Up @@ -682,18 +676,12 @@ public function withAvg($relation, $column)
/**
* Add subselect queries to include the existence of related models.
*
* @param mixed $relations
* @param string|array $relation
* @return $this
*/
public function withExists($relations)
public function withExists($relation)
{
$relations = is_array($relations) ? $relations : func_get_args();

if (count($relations) === 2 && $relations[1] instanceof Closure) {
$relations = [$relations[0] => $relations[1]];
}

return $this->withAggregate($relations, '*', 'exists');
return $this->withAggregate($relation, '*', 'exists');
}

/**
Expand Down
58 changes: 10 additions & 48 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,22 +1110,11 @@ public function testWithCountAndMergedWheres()
{
$model = new EloquentBuilderTestModelParentStub;

$sql = 'select "id", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ? and "active" = ?) as "active_foo_count" from "eloquent_builder_test_model_parent_stubs"';

$wheres = function ($query) {
$query->where('bam', '>', 'qux');
};

$builder = $model->select('id')->withCount(['activeFoo' => $wheres]);

$this->assertSame($sql, $builder->toSql());
$this->assertEquals(['qux', true], $builder->getBindings());

$model = new EloquentBuilderTestModelParentStub;

$builder = $model->select('id')->withCount('activeFoo', $wheres);
$builder = $model->select('id')->withCount(['activeFoo' => function ($q) {
$q->where('bam', '>', 'qux');
}]);

$this->assertSame($sql, $builder->toSql());
$this->assertSame('select "id", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ? and "active" = ?) as "active_foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
$this->assertEquals(['qux', true], $builder->getBindings());
}

Expand Down Expand Up @@ -1205,17 +1194,9 @@ public function testWithCountMultipleAndPartialRename()
{
$model = new EloquentBuilderTestModelParentStub;

$sql = 'select "eloquent_builder_test_model_parent_stubs".*, (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_bar", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_count" from "eloquent_builder_test_model_parent_stubs"';

$builder = $model->withCount(['foo as foo_bar', 'foo']);

$this->assertSame($sql, $builder->toSql());

$model = new EloquentBuilderTestModelParentStub;

$builder = $model->withCount('foo as foo_bar', 'foo');

$this->assertSame($sql, $builder->toSql());
$this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_bar", (select count(*) from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

public function testWithExists()
Expand All @@ -1240,22 +1221,11 @@ public function testWithExistsAndMergedWheres()
{
$model = new EloquentBuilderTestModelParentStub;

$wheres = function ($query) {
$query->where('bam', '>', 'qux');
};

$sql = 'select "id", exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ? and "active" = ?) as "active_foo_exists" from "eloquent_builder_test_model_parent_stubs"';

$builder = $model->select('id')->withExists(['activeFoo' => $wheres]);

$this->assertSame($sql, $builder->toSql());
$this->assertEquals(['qux', true], $builder->getBindings());

$model = new EloquentBuilderTestModelParentStub;

$builder = $model->select('id')->withExists('activeFoo', $wheres);
$builder = $model->select('id')->withExists(['activeFoo' => function ($q) {
$q->where('bam', '>', 'qux');
}]);

$this->assertSame($sql, $builder->toSql());
$this->assertSame('select "id", exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and "bam" > ? and "active" = ?) as "active_foo_exists" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
$this->assertEquals(['qux', true], $builder->getBindings());
}

Expand Down Expand Up @@ -1313,17 +1283,9 @@ public function testWithExistsMultipleAndPartialRename()
{
$model = new EloquentBuilderTestModelParentStub;

$sql = 'select "eloquent_builder_test_model_parent_stubs".*, exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_bar", exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_exists" from "eloquent_builder_test_model_parent_stubs"';

$builder = $model->withExists(['foo as foo_bar', 'foo']);

$this->assertSame($sql, $builder->toSql());

$model = new EloquentBuilderTestModelParentStub;

$builder = $model->withExists('foo as foo_bar', 'foo');

$this->assertSame($sql, $builder->toSql());
$this->assertSame('select "eloquent_builder_test_model_parent_stubs".*, exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_bar", exists(select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id") as "foo_exists" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

public function testHasWithConstraintsAndHavingInSubquery()
Expand Down

0 comments on commit 113b04a

Please sign in to comment.