0

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!

Kirzilla
  • 15,130
  • 23
  • 80
  • 126
  • 1
    Everything programmed is a pain in the ass in the future. The only question is of degree. – Waleed Khan Oct 31 '12 at 12:52
  • 3
    you might find [this](http://stackoverflow.com/a/9685039/727208) and [this](http://stackoverflow.com/a/9688176/727208) answer relevant. – tereško Oct 31 '12 at 12:54
  • @tereško, thank you for your commet! About first link - I was topic starter and I really appreciate your answer! :) In ACL, I prefer Model (it's methods, of course) to be as Resource, not Controller's actions. Question not about this. Question about what is the correct way of interaction between "fat model" and "thin controller". PS: Sorry for my English. – Kirzilla Oct 31 '12 at 13:00
  • So you now have partially moved the controller logic into the model. Because of that you call it fat model? Why don't you call it `FatModelWithHalfAControllerUsingStaticSharedServiceRegistryThatHasAnythingElseButUsesNotTheViewTemplateThingy` ;) ? – hakre Oct 31 '12 at 15:17

0 Answers0