Answer the question
In order to leave comments, you need to log in
What is a model in OOP on the web?
Hello! I constantly return to this question from task to task, and depending on the required task, my model floats from side to side. Please explain on my example what should be considered a model and how to work with it. In the meantime, I will describe my thoughts on this matter. Please take them into account when answering, because it is important ...
On all resources and in all manuals, and myself, in principle, we figured out the controller and the view (there are, of course, inaccuracies, but I'll start).
The controller receives data from the user and simply sends the data to the model that can process it, everything seems to be clear here.
The model, having done something, returns the data to the controller, which sends this data to a certain class that knows how to process it. Using the example of displaying a page with a list of users, it turns out like this: the model returns an "array" (collection) that contains user objects (the user object is a separate class) that contains the fields name, address, avatar, etc. A view is a class that knows that it has received exactly an array of user objects and knows their methods. This means that the view class wedges ready-made data into the template (HTML). those. to display the first and last name, the UserEntity object contains a method that returns
. But now the model ... And then there are some misunderstandings ...
I imagined the model as a class that works with the database. For example, in the SearchUsers controller$this->getName().' '.$this->getLastName()
we call the methods of the model and indicate what data we need. . and then calls the method . The model builds a query to the database based on the received data. those. inside the model it is written that if the controller requested fields where there is a photo, then the model includes a field in the SELECT request, say, avatar . Yes, they are not the same, but I see this as an advantage. The controller does not know the names of the fields in the database, but can simply indicate what it needs, and the model understands and substitutes the real name of the field or several fields (for example, if the controller indicated that address is needed, then the model will request the country, state, city fields in the database) . Further, having received data from the database, the model executes an internal method that creates a UserEntity object from the data received from the database$this->model->setFields('name, last_name,photo');
$this->model->searchUsers()
$this->createObject()
and inserts into it those fields that she has (all others are specified by default in the UserEntity object ). Well, that's actually all her work. BUT in many comments I see messages like: "the model should not go into the database." But then who should climb into it? Yet again. Somewhere, the model is represented as my UserEntity object, which gets itself from the database using the ORM, but I can’t understand how it gets a whole list of users (itself).
Therefore, this question arose ...
But in my method, I also see problems ... If for one controller I want to get my UserEntity , and for another controller, another UserEntity objectwhich contains not only the data that users need to display, but also those that are needed in the admin panel, it is no longer possible. Because I will somehow need to pass the User object from the controller to the model so that it creates an object based on another class, but how to make it clear which fields are there and which are not is no longer clear ....
Answer the question
In order to leave comments, you need to log in
Controller, view, model are elements of MVC, they have nothing to do with OOP.
The controller receives a request from the user in a certain form (browser request, API call, command, all these are different groups of controllers). Parsing the request passes it to the model for processing.
The model contains the business logic, i.e. essentially what your application should do, without being tied to user interactions and outputs. Perhaps models working with the database will be called, or maybe they won’t, it doesn’t matter, don’t exalt the database as a super-entity, this is a common tool, one of many.
Next, the model passes the data prepared for output to the controller (in classic MVC, immediately to the view), and the controller passes it to the desired view.
That's all, a simple scheme that will allow you to separate flies from cutlets, create an API, modify the output without touching the main logic. Working with a database is a completely different matter.
Somehow everything is confusing with you, horses and people are mixed up.
Here's what I know.
You write in the question
Further, having received data from the database, the model executes the internal method $this->createObject(), which creates a UserEntity object from the received data from the database and inserts into it the fields that it has (all the rest are specified by default in the UserEntity object). Well, that's actually all her work. BUT in many comments I see messages like: "the model should not go into the database." But then who should climb into it?
You never mentioned the repository, it is so lacking in your monologue.
Terminology.
A model is some object that has fields, getters and setters - that's the place where we put the data and only them.
An entity is an Entity and has a special relationship with your database - usually each instance of an entity corresponds to a separate row. In a doctrine orm, a model can be described in an annotation.
Repository - a class corresponding to the Entity class and containing methods like getById, getMyHome, ...
Now having some kind of representation
If the model is just an object that stores some values, and the entity can be a class, or an annotation that describes relationships and field types. A model is just a class with getters and setters.
That is a repository over a model and an entity, God forgive me.
It is in the repository that you describe how you will pull your data from the database and as a result you will receive data that will be located in the model.
Here are some examples:
Method in a repository
/**
* @param int $id
* @return mixed
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function getById(int $id) {
return $this->createQueryBuilder("org")
->leftJoin("org.category", "cat")
->leftJoin("org.region", "region")->addSelect("region")
->leftJoin("org.country", "country")->addSelect("country")
->leftJoin("org.city", "city")->addSelect("city")
->leftJoin("org.street", "street")->addSelect("street")
->leftJoin("org.gps", "gps")->addSelect("gps")
->leftJoin("org.services", "services")->addSelect("services")
->leftJoin("services.group", "grp")->addSelect("grp")
->leftJoin("org.images", "images", "", "")->addSelect("images")
->leftJoin("org.reviews", "reviews", Join::WITH, "reviews.status = 1")->addSelect("reviews")
->where("org = :id")->setParameter("id", $id)
->getQuery()
->getSingleResult();
}
/**
* @Rest\Route(
* path="/organization.getById",
* name="organization_get_by_id",
* requirements={ "id"="\d+" }
* )
*
* @param Request $request
* @return object|JsonResponse
* @throws ExceptionInterface
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function getById(Request $request)
{
$organization_repository = $this->getDoctrine()->getManager()->getRepository("App:Organization");
$rs = new ResponseSchemaData();
$rs->setData($organization_repository->getById($request->get("id")));
$serializer = $this->get('serializer');
$response_object = $serializer->normalize($rs, "json", ["attributes" => [
"code",
"data" => [
"id",
"name",
"images",
"description",
"postCode",
"phone",
"email",
"home",
"site",
"schedule",
"images",
"rating",
"gps" => [
"latitude",
"longitude",
],
"category" => [
"id",
"name",
],
"reviews" => [
"id",
"author",
"text",
"likes",
"dislike",
"rating",
"createAt",
],
"services" => [
"id",
"name",
"types" => [
"id",
"name"
]
],
"country" => [
"id",
"name"
],
"region" => [
"id",
"name"
],
"city" => [
"id",
"name"
],
"street" => [
"id",
"name",
"streetType"
],
]
]]);
return new JsonResponse($response_object);
}
{
"data": {
"name": "Здравстолет",
"home": "51",
"phone": "8 (800) 350-29-32",
"site": "https://zdravstolet.ru/",
"schedule": "пн–пт 08:00–17:00",
"id": "641659",
"category": [],
"city": {
"id": 3170,
"name": "Бийск"
},
"street": {
"name": "Петра Мерлина",
"id": 19583,
"street_type": "улица"
},
"country": {
"name": "Россия",
"id": 3159
},
"reviews": [
{
"author": "пиаипиапи",
"text": "иапипиаиапипаипаиапи",
"likes": 0,
"dislike": 0,
"id": "41",
"rating": 0,
"create_at": "2019-09-22T20:03:42+00:00"
}
],
"description": null,
"email": null,
"rating": "3.7",
"services": [],
"images": [],
"post_code": 659303,
"region": null,
"gps": {
"latitude": 52.529974,
"longitude": 85.173845
}
},
"code": 0
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question