Skip to content

Commit

Permalink
Fix pivot timestamp columns without parent (#25746)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Sep 23, 2018
1 parent b70a678 commit a230ddb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ public function hasTimestampAttributes()
*/
public function getCreatedAtColumn()
{
return $this->pivotParent->getCreatedAtColumn();
return ($this->pivotParent)
? $this->pivotParent->getCreatedAtColumn()
: parent::getCreatedAtColumn();
}

/**
Expand All @@ -226,6 +228,8 @@ public function getCreatedAtColumn()
*/
public function getUpdatedAtColumn()
{
return $this->pivotParent->getUpdatedAtColumn();
return ($this->pivotParent)
? $this->pivotParent->getUpdatedAtColumn()
: parent::getUpdatedAtColumn();
}
}
28 changes: 28 additions & 0 deletions tests/Database/DatabaseEloquentPivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class DatabaseEloquentPivotTest extends TestCase
Expand Down Expand Up @@ -127,6 +128,29 @@ public function testPivotModelTableNameIsSingular()

$this->assertEquals('pivot', $pivot->getTable());
}

public function testPivotModelWithParentReturnsParentsTimestampColumns()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model');
$parent->shouldReceive('getCreatedAtColumn')->andReturn('parent_created_at');
$parent->shouldReceive('getUpdatedAtColumn')->andReturn('parent_updated_at');

$pivotWithParent = new Pivot();
$pivotWithParent->pivotParent = $parent;

$this->assertEquals('parent_created_at', $pivotWithParent->getCreatedAtColumn());
$this->assertEquals('parent_updated_at', $pivotWithParent->getUpdatedAtColumn());
}

public function testPivotModelWithoutParentReturnsModelTimestampColumns()
{
$model = new DummyModel();

$pivotWithoutParent = new Pivot();

$this->assertEquals($model->getCreatedAtColumn(), $pivotWithoutParent->getCreatedAtColumn());
$this->assertEquals($model->getUpdatedAtColumn(), $pivotWithoutParent->getUpdatedAtColumn());
}
}

class DatabaseEloquentPivotTestDateStub extends \Illuminate\Database\Eloquent\Relations\Pivot
Expand Down Expand Up @@ -160,3 +184,7 @@ class DatabaseEloquentPivotTestJsonCastStub extends \Illuminate\Database\Eloquen
'foo' => 'json',
];
}

class DummyModel extends Model
{
}

0 comments on commit a230ddb

Please sign in to comment.