Skip to content

Commit

Permalink
Rename to withCount, auto column names, default select
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed May 10, 2016
1 parent 8d64b76 commit 67b821d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
56 changes: 35 additions & 21 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,27 +762,6 @@ public function orWhere($column, $operator = null, $value = null)
return $this->where($column, $operator, $value, 'or');
}

/**
* Add a relationship count select to the query.
*
* @param string $relation
* @param string $as
* @param \Closure|null $callback
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function selectCount($relation, $as, Closure $callback = null)
{
$relation = $this->getHasRelationQuery($relation);

$query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);

if ($callback) {
call_user_func($callback, $query);
}

return $this->selectSub($query->getQuery(), $as);
}

/**
* Add a relationship count / exists condition to the query.
*
Expand Down Expand Up @@ -1020,6 +999,41 @@ public function with($relations)
return $this;
}

/**
* Add subselect queries to count the relations.
*
* @param mixed $relations
* @return $this
*/
public function withCount($relations)
{
// If no columns are set, add the default * columns.
if (is_null($this->query->columns)) {
$this->query->select(['*']);
}

if (is_string($relations)) {
$relations = func_get_args();
}

$relations = $this->parseWithRelations($relations);

foreach ($relations as $name => $constraints) {
// First determine the count query for the given relationship,
// then run the constraints callback to get the final query.
// This query will be added as subSelect query.
$relation = $this->getHasRelationQuery($name);
$query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);

call_user_func($constraints, $query);

$asColumn = snake_case($name).'_count';
$this->selectSub($query->getQuery(), $asColumn);
}

return $this;
}

/**
* Parse a list of relations into individuals.
*
Expand Down
25 changes: 17 additions & 8 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,25 +455,34 @@ public function testDeleteOverride()
$this->assertEquals(['foo' => $builder], $builder->delete());
}

public function testSelectCount()
public function testWithCount()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->selectCount('foo', 'fooCount');
$builder = $model->withCount('foo');

$this->assertEquals('select (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 "fooCount" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
$this->assertEquals('select *, (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 testSelectCountWithSelectAndContraintsAndHaving()
public function testWithCountAndSelect()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('bar', 'baz')->select('*');
$builder->selectCount('foo', 'fooCount', function ($q) {
$builder = $model->select('id')->withCount('foo');

$this->assertEquals('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") as "foo_count" from "eloquent_builder_test_model_parent_stubs"', $builder->toSql());
}

public function testWithCountAndContraintsAndHaving()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('bar', 'baz');
$builder->withCount(['foo' => function ($q) {
$q->where('bam', '>', 'qux');
})->having('fooCount', '>=', 1);
}])->having('fooCount', '>=', 1);

$this->assertEquals('select *, (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" > ?) as "fooCount" from "eloquent_builder_test_model_parent_stubs" where "bar" = ? having "fooCount" >= ?', $builder->toSql());
$this->assertEquals('select *, (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" > ?) as "foo_count" from "eloquent_builder_test_model_parent_stubs" where "bar" = ? having "fooCount" >= ?', $builder->toSql());
$this->assertEquals(['qux', 'baz', 1], $builder->getBindings());
}

Expand Down

1 comment on commit 67b821d

@patchthecode
Copy link

Choose a reason for hiding this comment

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

Does this work with nested eager loading?

Please sign in to comment.