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

[5.5] Clear CountQuery "select" bindings since we're overriding the columns anyway #21468

Merged
merged 6 commits into from
Sep 29, 2017
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function getRelationExistenceCountQuery(Builder $query, Builder $parentQu
{
return $this->getRelationExistenceQuery(
$query, $parentQuery, new Expression('count(*)')
);
)->setBindings([], 'select');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ public function testRelationCountQueryCanBeBuilt()

$builder->shouldReceive('select')->once()->with(m::type('Illuminate\Database\Query\Expression'))->andReturnSelf();
$relation->getParent()->shouldReceive('getTable')->andReturn('table');
$builder->shouldReceive('whereColumn')->once()->with('table.id', '=', 'table.foreign_key');
$builder->shouldReceive('whereColumn')->once()->with('table.id', '=', 'table.foreign_key')->andReturn($baseQuery);
$baseQuery->shouldReceive('setBindings')->once()->with([], 'select');

$relation->getRelationExistenceCountQuery($builder, $builder);
}
Expand Down
106 changes: 106 additions & 0 deletions tests/Integration/Database/EloquentWithCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentWithCountTest;

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentWithCountTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

public function setUp()
{
parent::setUp();

Schema::create('one', function ($table) {
$table->increments('id');
});

Schema::create('two', function ($table) {
$table->increments('id');
$table->integer('one_id');
});

Schema::create('three', function ($table) {
$table->increments('id');
$table->integer('two_id');
});
}

/**
* @test
*/
public function it_basic()
{
$one = Model1::create();
$two = $one->twos()->Create();
$three = $two->threes()->Create();

$results = Model1::withCount([
'twos' => function ($query) {
$query->where('id', '>=', 1);
},
]);

$this->assertEquals([
['id' => 1, 'twos_count' => 1],
], $results->get()->toArray());
}
}

class Model1 extends Model
{
public $table = 'one';
public $timestamps = false;
protected $guarded = ['id'];

public function twos()
{
return $this->hasMany(Model2::class, 'one_id');
}
}

class Model2 extends Model
{
public $table = 'two';
public $timestamps = false;
protected $guarded = ['id'];
protected $withCount = ['threes'];

public function threes()
{
return $this->hasMany(Model3::class, 'two_id');
}
}

class Model3 extends Model
{
public $table = 'three';
public $timestamps = false;
protected $guarded = ['id'];

protected static function boot()
{
parent::boot();

static::addGlobalScope('app', function ($builder) {
$builder->where('idz', '>', 0);
});
}
}