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:
CategoryController
gets an array of article ids from the CategoryModel
CategoryController
creates some ArticleModel
s/ArticleView
sCategoryController
tells each what data to loadCategoryController
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:
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