Skip to content

Even more fluent eloquent model via magic setters #1976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2013
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
7 changes: 4 additions & 3 deletions laravel/database/eloquent/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -769,7 +770,7 @@ public function __call($method, $parameters)
}
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
Expand Down
15 changes: 14 additions & 1 deletion laravel/tests/cases/eloquent.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -288,4 +301,4 @@ public function testConvertingToArray()

}

}
}