Skip to content

Commit

Permalink
Check UPDATED_AT column existence in HasOneOrMany::update() (#23747)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Mar 30, 2018
1 parent f5b94cc commit 707d669
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ protected function setForeignAttributesForCreate(Model $model)
*/
public function update(array $attributes)
{
if ($this->related->usesTimestamps()) {
if ($this->related->usesTimestamps() && ! is_null($this->relatedUpdatedAt())) {
$attributes[$this->relatedUpdatedAt()] = $this->related->freshTimestampString();
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ public function testUpdateMethodUpdatesModelsWithTimestamps()
$this->assertEquals('results', $relation->update(['foo' => 'bar']));
}

public function testUpdateMethodUpdatesModelsWithNullUpdatedAt()
{
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('usesTimestamps')->once()->andReturn(true);
$relation->getRelated()->shouldReceive('getUpdatedAtColumn')->andReturn(null);
$relation->getQuery()->shouldReceive('update')->once()->with(['foo' => 'bar'])->andReturn('results');

$this->assertEquals('results', $relation->update(['foo' => 'bar']));
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public function testUpdateMethodUpdatesModelsWithTimestamps()
$this->assertEquals('results', $relation->update(['foo' => 'bar']));
}

public function testUpdateMethodUpdatesModelsWithNullUpdatedAt()
{
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('usesTimestamps')->once()->andReturn(true);
$relation->getRelated()->shouldReceive('getUpdatedAtColumn')->andReturn(null);
$relation->getQuery()->shouldReceive('update')->once()->with(['foo' => 'bar'])->andReturn('results');

$this->assertEquals('results', $relation->update(['foo' => 'bar']));
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down

0 comments on commit 707d669

Please sign in to comment.