Skip to content

Commit 8d3910f

Browse files
committed
Fix custom pivot timestamp column names
1 parent 9f9d7f8 commit 8d3910f

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ class BelongsToMany extends Relation {
5050
*/
5151
protected $pivotWheres = [];
5252

53+
/**
54+
* The custom pivot table column for the created_at timestamp.
55+
*
56+
* @var array
57+
*/
58+
protected $pivotCreatedAt;
59+
60+
/**
61+
* The custom pivot table column for the updated_at timestamp.
62+
*
63+
* @var array
64+
*/
65+
protected $pivotUpdatedAt;
66+
5367
/**
5468
* Create a new belongs to many relationship instance.
5569
*
@@ -1129,7 +1143,30 @@ public function withPivot($columns)
11291143
*/
11301144
public function withTimestamps($createdAt = null, $updatedAt = null)
11311145
{
1132-
return $this->withPivot($createdAt ?: $this->createdAt(), $updatedAt ?: $this->updatedAt());
1146+
$this->pivotCreatedAt = $createdAt;
1147+
$this->pivotUpdatedAt = $updatedAt;
1148+
1149+
return $this->withPivot($this->createdAt(), $this->updatedAt());
1150+
}
1151+
1152+
/**
1153+
* Get the name of the "created at" column.
1154+
*
1155+
* @return string
1156+
*/
1157+
public function createdAt()
1158+
{
1159+
return $this->pivotCreatedAt ?: $this->parent->getCreatedAtColumn();
1160+
}
1161+
1162+
/**
1163+
* Get the name of the "updated at" column.
1164+
*
1165+
* @return string
1166+
*/
1167+
public function updatedAt()
1168+
{
1169+
return $this->pivotUpdatedAt ?: $this->parent->getUpdatedAtColumn();
11331170
}
11341171

11351172
/**

tests/Database/DatabaseEloquentBelongsToManyTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ public function testAttachInsertsPivotTableRecordWithTimestampsWhenNecessary()
180180
}
181181

182182

183+
public function testAttachInsertsPivotTableRecordWithCustomTimestampColumns()
184+
{
185+
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', array('touchIfTouching'), $this->getRelationArguments());
186+
$relation->withTimestamps('custom_created_at', 'custom_updated_at');
187+
$query = m::mock('stdClass');
188+
$query->shouldReceive('from')->once()->with('user_role')->andReturn($query);
189+
$query->shouldReceive('insert')->once()->with(array(array('user_id' => 1, 'role_id' => 2, 'foo' => 'bar', 'custom_created_at' => 'time', 'custom_updated_at' => 'time')))->andReturn(true);
190+
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
191+
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
192+
$relation->getParent()->shouldReceive('freshTimestamp')->once()->andReturn('time');
193+
$relation->expects($this->once())->method('touchIfTouching');
194+
195+
$relation->attach(2, array('foo' => 'bar'));
196+
}
197+
198+
183199
public function testAttachInsertsPivotTableRecordWithACreatedAtTimestamp()
184200
{
185201
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', array('touchIfTouching'), $this->getRelationArguments());

0 commit comments

Comments
 (0)