0

In MVC, which layer determines which (dynamic) data are loaded into a model?

In my web application, the ArticleView displays an "author card," or metadata about the user who wrote the article. In my current setup, the UserModel decides what data to pull from the DB for the author card in a function called getCardData:

class UserModel {
    public function getCardData() {
       $cardFields = ['firstName', 'lastName', 'avatarChoice', 'email', 'facebookId'];
       $cardData = $this->loadDataFromPersistenceLayer($cardFields);
       return $cardData;
    }
}

I’m not sure if this is incorrect, perhaps this function instead should go in the view? or the controller?

Fuzzier to me is something like a dynamic category archive page, which would contain a collection of articles. Trying to work it out:

  • the CategoryController gets an array of article ids from the CategoryModel
  • CategoryController creates some ArticleModels/ArticleViews
  • CategoryController tells each what data to load
  • CategoryController renders each ArticleView
    • $data = $articleView->renderCategoryPageTemplate();
  • CategoryController passes that rendered data to CategoryView which incorporates it into its output.

No matter how I think about it, it seems wrong to me.

Notes, if it matters:

  • I have a Redis layer that I prefer to hit instead of the DB when possible for metadata
  • There are user- and article-meta tables to support dynamic properties, so I’m not dealing with fixed columns so to speak
moon prism power
  • 2,347
  • 2
  • 15
  • 24
  • Why do you think its incorrect? Perhaps maybe an `articleModel` for your `getCardData()` method and all in-between but definitely not putting it in the view or the controller. – Lawrence Cherone Jul 21 '14 at 23:08
  • @LozCheroneツ my train of thought was this method is only to be called from one page template, so does it make sense to put it in the User Model?And what if I decide that the author card should display more information, should it be up to the User Model to change its own method? – moon prism power Jul 22 '14 at 00:05
  • 1
    Model is not a class and there are no such thing as "models". Model is a layer itself, containing several groups of classes. Please read [this](http://stackoverflow.com/a/5864000/727208). – tereško Jul 22 '14 at 03:53

1 Answers1

-1

Based on my knowledge, deciding what goes where or when or ... is related to your business logic and as much as it's kept independent of your underlying layer or system you can put it anywhere.
Keeping independent from underlying system(or layer) means even if you some day decided to migrate your mvc framework to something else, your business logic can be kept intact

madz
  • 1,803
  • 18
  • 45
  • Hopefully I'd not bother myself deciding what should go precisely where as well as a -1. The way he asked that question made it identical for a comment or an answer's place. – madz Jul 22 '14 at 10:54