Merge pull request #1976 from neoascetic/more_fluent_eloquent

Even more fluent eloquent model via magic setters
This commit is contained in:
Taylor Otwell 2013-05-13 22:43:15 -07:00
commit 86fc0ca7cb
2 changed files with 18 additions and 4 deletions

View File

@ -562,11 +562,12 @@ public function get_attribute($key)
* *
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @return void * @return Model
*/ */
public function set_attribute($key, $value) public function set_attribute($key, $value)
{ {
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
return $this;
} }
/** /**
@ -769,7 +770,7 @@ public function __call($method, $parameters)
} }
elseif (starts_with($method, 'set_')) elseif (starts_with($method, 'set_'))
{ {
$this->set_attribute(substr($method, 4), $parameters[0]); return $this->set_attribute(substr($method, 4), $parameters[0]);
} }
// Finally we will assume that the method is actually the beginning of a // Finally we will assume that the method is actually the beginning of a

View File

@ -133,6 +133,19 @@ public function testAttributeMagicSetterMethodChangesAttribute()
Model::$accessible = null; Model::$accessible = null;
} }
/**
* Test the Model::__set method allows chaining.
*
* @group laravel
*/
public function testAttributeMagicSetterMethodAllowsChaining()
{
$model = new Model;
$this->assertInstanceOf('Model', $model->set_foo('foo'));
$model->set_bar('bar')->set_baz('baz');
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), $model->to_array());
}
/** /**
* Test the Model::__get method. * Test the Model::__get method.
* *