Answer the question
In order to leave comments, you need to log in
How to get composite data from the database and add them into one entity?
Hello!
There was such a confusion in my head. There are several pages on which posts are displayed. Posts, respectively, have an author, title and text + related data like date, number of views, etc.
It turns out that in order to correctly display the post on the entire site, we need one data point. Let's say the PostsModel class is that class. It works with the database and does everything related to posts. And here is just the problem.
We need to pass to the PostsView class , which is responsible for displaying the post as HTML, not only the post data, but also data about the author. This means that the User object should also be obtained at one point, for example UsersModel .
'Cause we can't just take andPostsModel JOIN required fields from Users table ? Doesn't that violate the Single Responsibility Rules? those. post should not know anything about user fields.
So there is an option to get the posts data, create objects from them, put them in the posts collection, and create a method in the collection that can add the User object to the desired post of the collection. I hope I speak correctly.
After that, we request in the UsersModel model all the users who were the authors of the selected posts and put them in each post if there are matches.
But in this case, it turns out that the post view will communicate with the user's methods.
Please direct me to the right path, I'm completely confused.
What to read on this topic, maybe some patterns or where to peep ...
Answer the question
In order to leave comments, you need to log in
Для класса PostsModel:
Автор Поста является потомком Поста.
Пост для автора - Родитель.
Пост для даты поста - Родитель.
For the PostsModel class:
The Post Author is a descendant of the Post.
Post for the author - Parent.
Post for post date - Parent.
etc.
Inside the Post object, you can create a new User object, pass the user id into it (the user id is specified for each post, that is, who the author is) and then display the user name and everything else. In short, "composition": an association in which the object used is created within the class.
As a result, you will have one collection of posts and everything will be in it, and not two "heaps" - Posts and Users.
PostsView, which is responsible for displaying the post as HTMLthere should not be any PostsView - just one View is, in fact, a template engine with output buffering. What do you think PostsView should do?
What to read on this topicFowler's Enterprise Application Architecture. Chapter "data sources".
After all, we can’t just take and JOIN the necessary fields from the Users table in the PostsModel, can we?I did just that in my self-written framework. There is a method for JOIN in my ORM, it returns a multi-dimensional array of objects, where each end element (in your example) will be a Post and User model object. Why is that? Because to do this:
<html>
Текст поста: <?=$post->getText()?><br>
Автор поста: <?=$post->getAuthor()->getName()?> <!-- это самое сложное -->
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question