A
A
Alexey Konovalov2019-09-22 22:52:15
PHP
Alexey Konovalov, 2019-09-22 22:52:15

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

4 answer(s)
E
Evgeny Romashkan, 2019-09-23
@EvgeniiR

Slightly not an answer
Для класса PostsModel:
Автор Поста является потомком Поста.
Пост для автора - Родитель.
Пост для даты поста - Родитель.

*Тут была реплика про уровень комьюнити тостера, и даже "Кураторов Тега", которые позволяют себе нести вышепроцитированную чушь, но я решил её опустить, т.к. разумный человек и так поймёт со временем*
Совет - развивайте критическое мышление и фильтруйте информацию, не верьте всему в интернете, даже если у этого 100500 плюсов на каком-нибудь ресурсе типа Хабра/SO/Medium/Тостер и т.п.

Alexey Konovalov ,
We don't need a "single point of data acquisition". You have your models (entities, domain models) - this is a component of your system responsible for part of the business logic.
You also have data views. There can be many representations of data. No need to reuse the same model for logic/writing and for reading (views, UI).
Selected data from the database (raw SQL/Eloquent/DQL mapped to DTO, whatever)
-> filled them with a structure that was formed based on the needs of the client module (your Frontend),
-> led to the desired format (json etc.) and gave to the frontend
It's very good that you noticed that this is a problem, seriously. That is why "Active Record" is an anti-pattern.
Some, unfortunately, cannot come to this for years, even more - they defend every architectural solution of some Laravel not from the position of an engineer, but from the position of a religious fanatic.
Models for writing do not need to know anything about HTTP, Request and views. Read models are essentially just data structures.

X
xmoonlight, 2019-09-22
@xmoonlight

For the PostsModel class:
The Post Author is a descendant of the Post.
Post for the author - Parent.
Post for post date - Parent.
etc.

A
Anton R., 2019-09-23
@anton_reut

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.

N
Northern Lights, 2019-09-23
@php666

PostsView, which is responsible for displaying the post as HTML
there 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 topic
Fowler'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>

- to do it on your own is a very, very non-trivial task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question