From 3a46721eca6562f518c65a8ecc00af8816e14da3 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Wed, 8 May 2013 18:19:27 +1100 Subject: [PATCH 1/2] 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 --- laravel/database/eloquent/model.php | 1 + laravel/tests/cases/eloquent.test.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 23d25b02528..b35fb1a2b05 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -770,6 +770,7 @@ public function __call($method, $parameters) elseif (starts_with($method, 'set_')) { $this->set_attribute(substr($method, 4), $parameters[0]); + return $this; } // Finally we will assume that the method is actually the beginning of a diff --git a/laravel/tests/cases/eloquent.test.php b/laravel/tests/cases/eloquent.test.php index ec08ed0fe95..6111eb469ed 100644 --- a/laravel/tests/cases/eloquent.test.php +++ b/laravel/tests/cases/eloquent.test.php @@ -133,6 +133,19 @@ public function testAttributeMagicSetterMethodChangesAttribute() 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. * @@ -288,4 +301,4 @@ public function testConvertingToArray() } -} \ No newline at end of file +} From f2f1d4d17301cde3cb62ea1f5b4cbf6ba8172f10 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Wed, 8 May 2013 20:11:44 +1000 Subject: [PATCH 2/2] Return `$this` in `set_attribute` --- laravel/database/eloquent/model.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index b35fb1a2b05..6c0ef483338 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -441,7 +441,7 @@ public function timestamp() } /** - *Updates the timestamp on the model and immediately saves it. + * Updates the timestamp on the model and immediately saves it. * * @return void */ @@ -562,11 +562,12 @@ public function get_attribute($key) * * @param string $key * @param mixed $value - * @return void + * @return Model */ public function set_attribute($key, $value) { $this->attributes[$key] = $value; + return $this; } /** @@ -769,8 +770,7 @@ public function __call($method, $parameters) } elseif (starts_with($method, 'set_')) { - $this->set_attribute(substr($method, 4), $parameters[0]); - return $this; + return $this->set_attribute(substr($method, 4), $parameters[0]); } // Finally we will assume that the method is actually the beginning of a