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 theCategoryModel
CategoryController
creates someArticleModel
s/ArticleView
sCategoryController
tells each what data to loadCategoryController
renders eachArticleView
$data = $articleView->renderCategoryPageTemplate();
CategoryController
passes that rendered data toCategoryView
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