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 "select" bindings whenever select columns have been override. #21486

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public function select($columns = ['*'])
{
$this->columns = is_array($columns) ? $columns : func_get_args();

$this->setBindings([], 'select');

return $this;
}

Expand Down
1 change: 0 additions & 1 deletion tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ 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')->andReturn($baseQuery);
$baseQuery->shouldReceive('setBindings')->once()->with([], 'select');

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

namespace Illuminate\Tests\Integration\Database\QueryBuilderOverrideSelectTest;

use Orchestra\Testbench\TestCase;

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

/**
* @test
*/
public function test_query_builder_can_clear_bindings_when_override_select_columns()
{
$query = app('Illuminate\Database\Query\Builder')
->selectRaw('(select count(*) from posts where status = ?)', [1])
->from('users');

$this->assertEquals([1], $query->getBindings());

$query = $query->select()->where('foo', 'bar');

$this->assertEquals(['bar'], $query->getBindings());
}
}