Answer the question
In order to leave comments, you need to log in
Is such an implementation possible (backend php)?
It is required to get data for a certain type, which are taken from the database.
Schema description:
-controller gives a request to get data for table1.
-repository determines what data is required for table 1 and gives the same variables to the class that performs database queries.
-data is returned to the repository, where, if necessary, something is processed, creates models and gives it to the controller.
Question: how to properly organize the definition of the required variables and, accordingly, the organization of the sql queries themselves. after all, there can be hundreds of variables, and for each variable to add your own request to the file, I think it’s not right, because they can differ only in the arguments for the filters in the request.
Answer the question
In order to leave comments, you need to log in
The question is not very clear. If the problem is that the same request is required to obtain different data, then of course this request should not be duplicated. The request will be one, and the "repository" will execute it with different incoming data.
True, traditionally "file with SQL queries" is usually called a repository, and "repository" - a service or helper.
In general, the normal structure is
1. Class "requests to the database". "Pull" the connection to the database
2. Repository class. Pulls the class "requests to the database" to itself. Each method returns a data structure, very loosely speaking - a "model".
3. The controller executes the methods of the repository, getting "models", and forming "variables" based on data from these "models". At the same time , it is NOT necessaryso that one "variable" corresponds to one "model", and for each "model" a separate query is written.
A model can contain many variables at once. For example, aggregating moguls can immediately return both the average and the maximum and the minimum value. And the controller will already decide what it needs
. Most importantly, requests can be generated dynamically ! If you have a query that displays data for all time, just add a condition to it , that if dates are passed to the repository method, then a date selection is added to the query. And immediately it turns out that there is only one method in the repository!
The same applies to other conditions. Need only blue irons? No problem - same methoda check for the presence of color in the conditions is added, and if there is, this condition is added to the request.
i.e. requests
SELECT count(field) FROM table WHERE a<100;
SELECT count(field) FROM table WHERE a=1 AND field_1 = 'd';
SELECT count(field) FROM table WHERE a=1 AND field_1 = 'd' and date between 2019 and 2020;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question