Skip to content

Commit 705ccb7

Browse files
committed
Use unguarded method for forceFill and forceCreate
1 parent f989a47 commit 705ccb7

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,15 @@ public function fill(array $attributes)
416416
*/
417417
public function forceFill(array $attributes)
418418
{
419-
static::unguard();
420-
421-
$this->fill($attributes);
422-
423-
static::reguard();
419+
// Since some versions of PHP have a bug that prevents it from properly
420+
// binding the late static context in a closure, we will first store
421+
// the model in a variable, which we will then use in the closure.
422+
$model = $this;
424423

425-
return $this;
424+
return static::unguarded(function() use ($model, $attributes)
425+
{
426+
return $model->fill($attributes);
427+
});
426428
}
427429

428430
/**
@@ -537,18 +539,15 @@ public static function create(array $attributes)
537539
*/
538540
public static function forceCreate(array $attributes)
539541
{
540-
if (static::$unguarded)
541-
{
542-
return static::create($attributes);
543-
}
544-
545-
static::unguard();
542+
// Since some versions of PHP have a bug that prevents it from properly
543+
// binding the late static context in a closure, we will first store
544+
// the model in a variable, which we will then use in the closure.
545+
$model = new static;
546546

547-
$model = static::create($attributes);
548-
549-
static::reguard();
550-
551-
return $model;
547+
return static::unguarded(function() use ($model, $attributes)
548+
{
549+
return $model->create($attributes);
550+
});
552551
}
553552

554553
/**

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ public function testCreateMethodSavesNewModel()
105105
}
106106

107107

108+
public function testForceCreateMethodSavesNewModelWithGuardedAttributes()
109+
{
110+
$_SERVER['__eloquent.saved'] = false;
111+
$model = EloquentModelSaveStub::forceCreate(['id' => 21]);
112+
$this->assertTrue($_SERVER['__eloquent.saved']);
113+
$this->assertEquals(21, $model->id);
114+
}
115+
116+
108117
public function testFindMethodCallsQueryBuilderCorrectly()
109118
{
110119
$result = EloquentModelFindStub::find(1);
@@ -705,6 +714,13 @@ public function testFillable()
705714
}
706715

707716

717+
public function testForceFillMethodFillsGuardedAttributes()
718+
{
719+
$model = (new EloquentModelSaveStub)->forceFill(['id' => 21]);
720+
$this->assertEquals(21, $model->id);
721+
}
722+
723+
708724
public function testUnguardAllowsAnythingToBeSet()
709725
{
710726
$model = new EloquentModelStub;
@@ -1300,7 +1316,7 @@ public function getDates()
13001316

13011317
class EloquentModelSaveStub extends Model {
13021318
protected $table = 'save_stub';
1303-
protected $guarded = array();
1319+
protected $guarded = ['id'];
13041320
public function save(array $options = array()) { $_SERVER['__eloquent.saved'] = true; }
13051321
public function setIncrementing($value)
13061322
{

0 commit comments

Comments
 (0)