Recently some people have given Models some more attention, for example see these excellent posts
http://blog.astrumfutura.com/archives/3 ... iated.html
http://weierophinney.net/matthew/archiv ... cture.html
The way I want to build my models is making them as fat as needed, to ensure all responsibilities of the models are in fact in the model. For example, I want the validation rules and filters to be in my model classes, not spread out over several controller files using the model classes.
So you can do
Code: Select all
class User {
/* */
}
// set a value and save
$user = new User();
$user->set('name','mike');
// ask the model if the data is valid
if($user->isValid()){
$user->save();
}
Code: Select all
class User {
protected $fields = array();
protected $rules = array();
protected $filters = arrayt();
public function __construct(){
// initiate fields and rules and filters for each field here
}
public function set ($field, $value) {}
public function get ($field) {}
public function save(){
// insert code to save the field data to for example a user table in db
}
}
How do you handle these kinds of more complex business models? I have read many tutorials and books but all examples so far deal with simple one-on-one relationships between classes and tables, which is relatively simple (even though the examples in Mathews blog posts are complex enough). And I haven't even started about using (nested) transactions or foreign key restrictions in the models (assuming the use of a database, but in theory the model will be independent from the storage).
What I'm looking for is some general thoughts about this, maybe a few snippets of code or links to more complex examples.