Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ public function __set($key, $value)
*/
public function offsetExists($offset)
{
return isset($this->$offset);
return ! is_null($this->getAttribute($offset));
}

/**
Expand All @@ -1265,7 +1265,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
return $this->$offset;
return $this->getAttribute($offset);
}

/**
Expand All @@ -1277,7 +1277,7 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
$this->$offset = $value;
$this->setAttribute($offset, $value);
}

/**
Expand All @@ -1288,7 +1288,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
unset($this->$offset);
unset($this->attributes[$offset], $this->relations[$offset]);
}

/**
Expand All @@ -1299,7 +1299,7 @@ public function offsetUnset($offset)
*/
public function __isset($key)
{
return ! is_null($this->getAttribute($key));
return $this->offsetExists($key);
}

/**
Expand All @@ -1310,7 +1310,7 @@ public function __isset($key)
*/
public function __unset($key)
{
unset($this->attributes[$key], $this->relations[$key]);
$this->offsetUnset($key);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Support/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function toJson($options = 0)
*/
public function offsetExists($offset)
{
return isset($this->{$offset});
return isset($this->attributes[$offset]);
}

/**
Expand All @@ -105,7 +105,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
return $this->{$offset};
return $this->get($offset);
}

/**
Expand All @@ -117,7 +117,7 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
$this->attributes[$offset] = $value;
}

/**
Expand All @@ -128,7 +128,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
unset($this->attributes[$offset]);
}

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ public function __get($key)
*/
public function __set($key, $value)
{
$this->attributes[$key] = $value;
$this->offsetSet($key, $value);
}

/**
Expand All @@ -176,7 +176,7 @@ public function __set($key, $value)
*/
public function __isset($key)
{
return isset($this->attributes[$key]);
return $this->offsetExists($key);
}

/**
Expand All @@ -187,6 +187,6 @@ public function __isset($key)
*/
public function __unset($key)
{
unset($this->attributes[$key]);
$this->offsetUnset($key);
}
}
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportFluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down