Commit 3a46721e by Pavel Puchkin

Even more fluent eloquent model via magic setters

Now it is possible to use Eloquent's magic setters in chains. For example:

    $model->set_foo('foo')->take(10)->set_bar(some_function());
    // ...even though setters 'foo' and 'bar' are not defined on the model
parent cffded66
...@@ -770,6 +770,7 @@ abstract class Model { ...@@ -770,6 +770,7 @@ abstract class Model {
elseif (starts_with($method, 'set_')) elseif (starts_with($method, 'set_'))
{ {
$this->set_attribute(substr($method, 4), $parameters[0]); $this->set_attribute(substr($method, 4), $parameters[0]);
return $this;
} }
// 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
......
...@@ -134,6 +134,19 @@ class EloquentTest extends PHPUnit_Framework_TestCase { ...@@ -134,6 +134,19 @@ class EloquentTest extends PHPUnit_Framework_TestCase {
} }
/** /**
* 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.
* *
* @group laravel * @group laravel
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment