8000 Make sure mutators also are called from pivot constructor · laravel/framework@b023247 · GitHub
[go: up one dir, main page]

Skip to content

Commit b023247

Browse files
committed
Make sure mutators also are called from pivot constructor
1 parent cb6902f commit b023247

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public function __construct(Model $parent, $attributes, $table, $exists = false)
4949
// The pivot model is a "dynamic" model since we will set the tables dynamically
5050
// for the instance. This allows it work for any intermediate tables for the
5151
// many to many relationship that are defined by this developer's classes.
52-
$this->setRawAttributes($attributes, true);
52+
$this->forceFill($attributes);
53+
$this->syncOriginal();
5354

5455
$this->setTable($table);
5556

tests/Database/DatabaseEloquentPivotTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public function testPropertiesAreSetCorrectly()
2323
$this->assertTrue($pivot->exists);
2424
}
2525

26+
public function testMutatorsAreCalledFromConstructor() {
27+
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
28+
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
29+
30+
$pivot = new DatabaseEloquentPivotTestMutatorStub($parent, array('foo' => 'bar'), 'table', true);
31+
32+
$this->assertTrue($pivot->getMutatorCalled());
33+
}
34+
2635

2736
public function testPropertiesUnchangedAreNotDirty()
2837
{
@@ -99,3 +108,16 @@ public function getDates()
99108
return array();
100109
}
101110
}
111+
112+
class DatabaseEloquentPivotTestMutatorStub extends Illuminate\Database\Eloquent\Relations\Pivot {
113+
private $mutatorCalled = false;
114+
115+
public function setFooAttribute($value) {
116+
$this->mutatorCalled = true;
117+
return $value;
118+
}
119+
120+
public function getMutatorCalled() {
121+
return $this->mutatorCalled;
122+
}
123+
}

0 commit comments

Comments
 (0)
0