Answer the question
In order to leave comments, you need to log in
How to separate query logic in DBMS and PHP?
Previously, when there were small projects and there was no time to devote proper time to learning MySQL, I did a simple or a couple of simple queries in MySQL, then I processed the result in PHP, perhaps making a couple more queries in a loop. Naturally, this is wrong. Began to study denser DBMS. The new versions have a lot of features, including conditions and more.
So, how not to overload the database query logic? How to properly separate the work of DBMS and PHP?
Answer the question
In order to leave comments, you need to log in
1. Controller or analogues.
2. Service level.
3. Business logic
layer 4. Data storage layer
Layers 2-4 are the model, layers 3-4 communicate through requests and commands.
How to properly separate the work of DBMS and PHP?
I all the same would try to give the process of data sampling to the DBMS as much as possible. This option is definitely faster and more reliable.
Everyone has a different definition of "correct". In essence, it all boils down to "either all or nothing."
If you have an understanding of how the DBMS works and its capabilities, you will most likely put everything into it: the application will only "go there for data" (pull one "storage" of all the heaps of requests), and write data, again through the "storage" and one request instead of a heap. We even have "schedulers" in the database itself (if they have to change the data or do something with it).
Or the option when the DBMS acts as a string store with advanced features such as secondary keys and indexes + more spreading syntax for "select", this is usually with MySql in view of its "features"
In addition to what Vyacheslav said:
1. If the calculations are carried out at the database level, then this introduces additional time and complexity costs in the development of unit tests.
2. Theoretically - it will also be problematic to migrate to another database, say MongoDB, for example. After all, the calculations are hardcoded in a certain engine. And so you have to duplicate the logic of calculations in the new engine, which is not very correct.
3. There is such a principle - it is called SoC. So, he says that responsibilities should be shared. Business logic and calculations are one responsibility, but data storage is another.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question