I'm trying to apply "fat model" principle, but I have no clue how to make interaction between "fat model" and controller correctly.
Let's suppose that we have Model_User
, Controller_User
and Model_User_Resource
(interacting with Db).
class Model_User
{
public function register($data, $sendRegistrationEmail = false) {
//checking ACL rules
if (!$this->getAcl()->checkAcl('registration')) {
throw new Acl_Exception();
}
//validating form
$F = new Form_User_Registration($data);
if (!$F->isValid()) {
return $F;
}
//inserting data and returning new user's id
$Resource = new Model_User_Resource();
$userId = $Resource->insert($data);
return $userId;
}
}
class Controller_User
{
public function registrationAction() {
$post = $this->getRequest->getPost();
$Model = new Model_User();
$result = $Model->register($post);
if ($result instanceof Form_User_Registration) {
//model has returned Form instance
return new Response(json_encode($result->getErrors()));
}
//registration was successful and we're doing some kind of redirect here, I suppose
}
}
So, what's wrong with this code? Model could: throw Acl exception, return Form instance if some errors occured, return integer if success or any other exceptions. My question is: Is it correct that Model is returning errors in this way? What is the best practice? Could this way be pain in the ass in the future?
Thank you!