Skip to content

Commit 4bc949c

Browse files
themsaidmichaeltintiuc
authored andcommitted
[5.5] Clear CountQuery "select" bindings since we're overriding the columns anyway (laravel#21468)
* update test * handle withCount edge case * fix style * fix style
1 parent 0bccce0 commit 4bc949c

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

src/Illuminate/Database/Eloquent/Relations/Relation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function getRelationExistenceCountQuery(Builder $query, Builder $parentQu
188188
{
189189
return $this->getRelationExistenceQuery(
190190
$query, $parentQuery, new Expression('count(*)')
191-
);
191+
)->setBindings([], 'select');
192192
}
193193

194194
/**

tests/Database/DatabaseEloquentHasOneTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public function testRelationCountQueryCanBeBuilt()
176176

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

181182
$relation->getRelationExistenceCountQuery($builder, $builder);
182183
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database\EloquentWithCountTest;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use Illuminate\Support\Facades\Schema;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
/**
10+
* @group integration
11+
*/
12+
class EloquentWithCountTest extends TestCase
13+
{
14+
protected function getEnvironmentSetUp($app)
15+
{
16+
$app['config']->set('app.debug', 'true');
17+
18+
$app['config']->set('database.default', 'testbench');
19+
20+
$app['config']->set('database.connections.testbench', [
21+
'driver' => 'sqlite',
22+
'database' => ':memory:',
23+
'prefix' => '',
24+
]);
25+
}
26+
27+
public function setUp()
28+
{
29+
parent::setUp();
30+
31+
Schema::create('one', function ($table) {
32+
$table->increments('id');
33+
});
34+
35+
Schema::create('two', function ($table) {
36+
$table->increments('id');
37+
$table->integer('one_id');
38+
});
39+
40+
Schema::create('three', function ($table) {
41+
$table->increments('id');
42+
$table->integer('two_id');
43+
});
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function it_basic()
50+
{
51+
$one = Model1::create();
52+
$two = $one->twos()->Create();
53+
$three = $two->threes()->Create();
54+
55+
$results = Model1::withCount([
56+
'twos' => function ($query) {
57+
$query->where('id', '>=', 1);
58+
},
59+
]);
60+
61+
$this->assertEquals([
62+
['id' => 1, 'twos_count' => 1],
63+
], $results->get()->toArray());
64+
}
65+
}
66+
67+
class Model1 extends Model
68+
{
69+
public $table = 'one';
70+
public $timestamps = false;
71+
protected $guarded = ['id'];
72+
73+
public function twos()
74+
{
75+
return $this->hasMany(Model2::class, 'one_id');
76+
}
77+
}
78+
79+
class Model2 extends Model
80+
{
81+
public $table = 'two';
82+
public $timestamps = false;
83+
protected $guarded = ['id'];
84+
protected $withCount = ['threes'];
85+
86+
public function threes()
87+
{
88+
return $this->hasMany(Model3::class, 'two_id');
89+
}
90+
}
91+
92+
class Model3 extends Model
93+
{
94+
public $table = 'three';
95+
public $timestamps = false;
96+
protected $guarded = ['id'];
97+
98+
protected static function boot()
99+
{
100+
parent::boot();
101+
102+
static::addGlobalScope('app', function ($builder) {
103+
$builder->where('idz', '>', 0);
104+
});
105+
}
106+
}

0 commit comments

Comments
 (0)