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.8] Fix ambiguous UPDATED_AT columns #26031

Merged
merged 2 commits into from
Oct 10, 2018
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
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,11 @@ protected function addUpdatedAtColumn(array $values)
return $values;
}

return Arr::add(
$values, $this->model->getUpdatedAtColumn(),
$this->model->freshTimestampString()
$column = $this->qualifyColumn($this->model->getUpdatedAtColumn());

return array_merge(
[$column => $this->model->freshTimestampString()],
$values
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function compileUpdate(Builder $query, $values)
// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = $this->compileUpdateColumns($values);
$columns = $this->compileUpdateColumns($query, $values);

$from = $this->compileUpdateFrom($query);

Expand All @@ -185,20 +185,23 @@ public function compileUpdate(Builder $query, $values)
/**
* Compile the columns for the update statement.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
protected function compileUpdateColumns($values)
protected function compileUpdateColumns($query, $values)
{
// When gathering the columns for an update statement, we'll wrap each of the
// columns and convert it to a parameter value. Then we will concatenate a
// list of the columns that can be added into this update query clauses.
return collect($values)->map(function ($value, $key) {
return collect($values)->map(function ($value, $key) use ($query) {
$column = Str::after($key, $query->from.'.');

if ($this->isJsonSelector($key)) {
return $this->compileJsonUpdateColumn($key, $value);
return $this->compileJsonUpdateColumn($column, $value);
}

return $this->wrap($key).' = '.$this->parameter($value);
return $this->wrap($column).' = '.$this->parameter($value);
})->implode(', ');
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use stdClass;
use Mockery as m;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -1049,6 +1050,24 @@ public function testOldestWithColumn()
$builder->oldest('foo');
}

public function testUpdate()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');

$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
$builder = new Builder($query);
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, '');
$builder->setModel($model);
$builder->getConnection()->shouldReceive('update')->once()
->with('update "table" set "table"."updated_at" = ?, "foo" = ?', [$now, 'bar'])->andReturn(1);

$result = $builder->update(['foo' => 'bar']);
$this->assertEquals(1, $result);

Carbon::setTestNow(null);
}

protected function mockConnectionForModel($model, $database)
{
$grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';
Expand Down Expand Up @@ -1085,6 +1104,11 @@ protected function getMockQueryBuilder()
}
}

class EloquentBuilderTestStub extends Model
{
protected $table = 'table';
}

class EloquentBuilderTestScopeStub extends Model
{
public function scopeApproved($query)
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ public function testUpdateMethodWithoutJoinsOnPostgres()
{
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
$result = $builder->from('users')->where('id', '=', 1)->update(['users.email' => 'foo', 'name' => 'bar']);
$this->assertEquals(1, $result);
}

Expand Down Expand Up @@ -2042,7 +2042,7 @@ public function testPostgresUpdateWrappingJson()
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('update')
->with('update "users" set "options" = jsonb_set("options"::jsonb, \'{"name","first_name"}\', ?)', ['"John"']);
$builder->from('users')->update(['options->name->first_name' => 'John']);
$builder->from('users')->update(['users.options->name->first_name' => 'John']);

$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('update')
Expand Down