Skip to content

Commit

Permalink
Fix qualified UPDATED_AT timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Dec 30, 2018
1 parent be2ebc4 commit 6484744
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,12 +857,18 @@ protected function addUpdatedAtColumn(array $values)
return $values;
}

$column = $this->qualifyColumn($this->model->getUpdatedAtColumn());
$column = $this->model->getUpdatedAtColumn();

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

$values[$this->qualifyColumn($column)] = $values[$column];

unset($values[$column]);

return $values;
}

/**
Expand Down
18 changes: 17 additions & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1081,14 +1081,30 @@ public function testUpdate()
$this->mockConnectionForModel($model, '');
$builder->setModel($model);
$builder->getConnection()->shouldReceive('update')->once()
->with('update "table" set "table"."updated_at" = ?, "foo" = ?', [$now, 'bar'])->andReturn(1);
->with('update "table" set "foo" = ?, "table"."updated_at" = ?', ['bar', $now])->andReturn(1);

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

Carbon::setTestNow(null);
}

public function testUpdateWithTimestampValue()
{
$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 "foo" = ?, "table"."updated_at" = ?', ['bar', null])->andReturn(1);

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

Carbon::setTestNow(null);
}

protected function mockConnectionForModel($model, $database)
{
$grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';
Expand Down

0 comments on commit 6484744

Please sign in to comment.