0

I'm using the MVC pattern in my application.

Now I need the view object in a model.

I don't want to add the view as a parameter for my function in the model (since I need it in other functions as well). And I don't want to keep on passing it.

Should a add the view as an attribute for the constructor of the model?

Is there another way? Shouldn't I be needing the view object in the model in the first place?

What would be the preferred way of doing it?

Example:

Controller

function someAction()
{
    $somemodel->add();
}

Model

class SomeModel()
{
    function add()
    {
        if ($view->user) {
            // do stuff
            $this->mail();
        } else {
            // do other stuff
        }
    }

    function mail()
    {
        Mailer::send($view->user->email, $this->getitems(), $view->layout);
    }

    function getitems()
    {
        return Items::getitems($view->user);
    }
}
3
  • 1
    Could you describe the problem rather than the solution you're trying to get working? As MVC is supposed to separate the view from the model (and the controller) and it seems there may be a better way. Commented Nov 4, 2011 at 0:05
  • @MartinBean: I need to access view variables in my model. The above is not a solution but is the problem. Commented Nov 4, 2011 at 0:09
  • 1
    why would you want to access your view variables within your model? This smells to me like something being out of its place... Commented Nov 4, 2011 at 0:59

3 Answers 3

5

If you're really doing MVC, then you won't need the view in the model, because only the controller should have access to the view.

Looking at the code you've provided, I can tell one thing: the add() method should not reference $view in any way (even for accessing its properties). Instead, the model should be provided with the $view->user value from the controller. The same goes for the mail() method.

Consider fixing those issues. Otherwise, you'll get into something worse later on.

Sign up to request clarification or add additional context in comments.

1 Comment

sound advice, I totally second it. Your model entities are what you pass around. IMHO the view object should be considered just as a repository for all items that you'll want to display. Something else and not the view shall be used for all other purposes. Just my 2cc.
1

The model should be separate from the view. So, as mkArtak said, the controller should be the only thing that communicates with the view. Which then passes only the necessary information to the model.

As for the model, it should really only deal with the information that it understands.

i.e. if you had a Car model... you don't want to build it dependent on it's factory. If you did, you would have to change your code if you wanted to build it in different factory.

Comments

1

The controller is where you 'bake' everything prepare for render. By bake I mean you consider any passed in $_REQUEST params, make model API calls to get the data you need, and set template variables to be rendered. Your action, at the end of this process should make a call to a template (view) you choose in order to render the 'baked' template variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.