Answer the question
In order to leave comments, you need to log in
PHP OOP object. How to select objects?
I study OOP (I ordered the book www.ozon.ru/context/detail/id/5648968 )
There is a common situation, let's say there is a "comment" object or better "article/post" . When you create an article object ( new Article ), everything is fine and clear:
it has a title, it has an author, it has a content, it has a summary, and so on.
What I can’t understand is how to make a selection of articles per page? Will it be an array of objects or a single object?
1) If an array of objects, then why do we need to "drag" the full content of the articles in the array of objects?
2) There is a field/property author_id - this is already the identifier of another "user" object. This is how the data should be loaded correctly for the article object (after all, it only knows everything about itself), but you need to load user_name, user_avatar from another entity.
Please, tell me with specific examples (I'll understand better) or direct me to an article where this is written.
Answer the question
In order to leave comments, you need to log in
Do you need to suggest a normal ORM? Doctrine ORM
1) because you don't know if you need it or not. This will be sorted out later when you post the article. Perhaps, according to your logic, there will be a conclusion of the full content of one article and the headings of the rest. Either way, it's the state of the object. For good, after "loading" the object should be in the state in which it was "saved". As if we just took the object added earlier from the array. Well, yes, due to proxy classes, the same Doctrine is able to partially load data and everything else, if necessary, with separate requests.
2) it's usually just an author field that stores a User object that already knows what's inside. But you still have relationships between objects.
It seems to me that it's too early for you to think about how to write your own ORM.
In fact, everything is complicated here.
Ideally, everything should be like this:
Because the object should, in theory, contain all the properties of the "Article" entity.
But all these wonderful theories are shattered by harsh reality. There are two main stumbling blocks:
Therefore, we go to all sorts of tricks. In particular, we agree with ourselves that our "article" object is a mapping of a record from a table. That is, it is rigidly tied to the structure of the table in the database. And then we make ourselves one more indulgence - when initializing the object, we can indicate that we only need the header. And then the code will create an object, then it will have only one property filled.
Accordingly, the answer to the first question is an array of objects, but truncated, with one property filled!
The answer to the second question lies in the same mapping: Describing our object as a mapping of a table in the database, we can also describe the relationships of this table! And then our object will be able to automatically "pull up" the related data if we refer to it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question