A
A
ADA M2014-11-03 17:23:53
Design patterns
ADA M, 2014-11-03 17:23:53

MVC, model for each table?

How to correctly build the architecture of MVC application models if, say, there is a need to use several tables, say, in some model class?
Example:
There are tables: classes and schedule.
I create a Schedule model whose getClassSchedule($class_id) method should get data from 2 of these tables (say, class schedule and class name).
So, from the point of view of building code, would it be correct to use calls to several tables in the Schedule model?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-11-05
Protko @Fesor

I create a Schedule model whose getClassSchedule($class_id) method should get data from these 2 tables

It's not a model, it's a service. A model is some kind of class that encapsulates data, this is the essence of which your business logic operates. If we were talking in the context of languages ​​like c# or Go - a model is a good old structure, just a structure ... in PHP there are only classes, because we have these classes.
That is, in your case, you have a Schedule service class that encapsulates the schedule management business logic for some entities. How it does it doesn't matter.
As for tables and models, ideally models are not tied in any way to how they are stored in the database. They are just classes. They are designed based on how easy it will be to work with them within the application. In the database, they can be stored in a completely different way, and other services should take over the storage task. Let's say you can have multiple models that inherit from a base class. For example, the base class Client is ultimately divided into retail and wholesale buyers, each has its own fields, some of the fields are common, some of the logic is common, for example, authorization .... it is logical to have a base class and two heirs. In the database, all this can be stored in one table, or in several, or something else ....
If you are interested, I advise you to read about DDD (Domain Driven Design).

D
Dmitry, 2014-11-03
@By_Engine

This is how I do it: I wrote a class for PDO and access different tables in models through it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question