diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 7169ef707ad6..2d8b206cb663 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -1254,7 +1254,7 @@ public function __set($key, $value) */ public function offsetExists($offset) { - return isset($this->$offset); + return ! is_null($this->getAttribute($offset)); } /** @@ -1265,7 +1265,7 @@ public function offsetExists($offset) */ public function offsetGet($offset) { - return $this->$offset; + return $this->getAttribute($offset); } /** @@ -1277,7 +1277,7 @@ public function offsetGet($offset) */ public function offsetSet($offset, $value) { - $this->$offset = $value; + $this->setAttribute($offset, $value); } /** @@ -1288,7 +1288,7 @@ public function offsetSet($offset, $value) */ public function offsetUnset($offset) { - unset($this->$offset); + unset($this->attributes[$offset], $this->relations[$offset]); } /** @@ -1299,7 +1299,7 @@ public function offsetUnset($offset) */ public function __isset($key) { - return ! is_null($this->getAttribute($key)); + return $this->offsetExists($key); } /** @@ -1310,7 +1310,7 @@ public function __isset($key) */ public function __unset($key) { - unset($this->attributes[$key], $this->relations[$key]); + $this->offsetUnset($key); } /** diff --git a/src/Illuminate/Support/Fluent.php b/src/Illuminate/Support/Fluent.php index f404cf8f7cd1..c34a5d760dad 100755 --- a/src/Illuminate/Support/Fluent.php +++ b/src/Illuminate/Support/Fluent.php @@ -94,7 +94,7 @@ public function toJson($options = 0) */ public function offsetExists($offset) { - return isset($this->{$offset}); + return isset($this->attributes[$offset]); } /** @@ -105,7 +105,7 @@ public function offsetExists($offset) */ public function offsetGet($offset) { - return $this->{$offset}; + return $this->get($offset); } /** @@ -117,7 +117,7 @@ public function offsetGet($offset) */ public function offsetSet($offset, $value) { - $this->{$offset} = $value; + $this->attributes[$offset] = $value; } /** @@ -128,7 +128,7 @@ public function offsetSet($offset, $value) */ public function offsetUnset($offset) { - unset($this->{$offset}); + unset($this->attributes[$offset]); } /** @@ -165,7 +165,7 @@ public function __get($key) */ public function __set($key, $value) { - $this->attributes[$key] = $value; + $this->offsetSet($key, $value); } /** @@ -176,7 +176,7 @@ public function __set($key, $value) */ public function __isset($key) { - return isset($this->attributes[$key]); + return $this->offsetExists($key); } /** @@ -187,6 +187,6 @@ public function __isset($key) */ public function __unset($key) { - unset($this->attributes[$key]); + $this->offsetUnset($key); } } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 9a3307e11cef..50951a3db275 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -98,6 +98,20 @@ public function testCalculatedAttributes() $this->assertEquals($hash, $model->password_hash); } + public function testArrayAccessToAttributes() + { + $model = new EloquentModelStub(['attributes' => 1, 'connection' => 2, 'table' => 3]); + unset($model['table']); + + $this->assertTrue(isset($model['attributes'])); + $this->assertEquals($model['attributes'], 1); + $this->assertTrue(isset($model['connection'])); + $this->assertEquals($model['connection'], 2); + $this->assertFalse(isset($model['table'])); + $this->assertEquals($model['table'], null); + $this->assertFalse(isset($model['with'])); + } + public function testNewInstanceReturnsNewInstanceWithAttributesSet() { $model = new EloquentModelStub; diff --git a/tests/Support/SupportFluentTest.php b/tests/Support/SupportFluentTest.php index 4216c3c3d3de..d1352bee56e7 100755 --- a/tests/Support/SupportFluentTest.php +++ b/tests/Support/SupportFluentTest.php @@ -58,6 +58,18 @@ public function testGetMethodReturnsAttribute() $this->assertNull($fluent->foo); } + public function testArrayAccessToAttributes() + { + $fluent = new Fluent(['attributes' => '1']); + + $this->assertTrue(isset($fluent['attributes'])); + $this->assertEquals($fluent['attributes'], 1); + + $fluent->attributes(); + + $this->assertTrue($fluent['attributes']); + } + public function testMagicMethodsCanBeUsedToSetAttributes() { $fluent = new Fluent;