G
G
GregIv2017-07-24 16:28:32
Design patterns
GregIv, 2017-07-24 16:28:32

What is M in MVC? What is the Active Record model? What should be in an AR model?

Good afternoon!
There is a debate about which is better a fat controller or a fat model? As a rule, they say that the model. However, many respected people argue that it is better - a thin controller, a thin model, + all the logic in separate classes.
Hmm... It is still often not clear in what context the word model is used?
As I understand it, a model in MVC is not just an Active Record model, but a set of all classes that work with data between a thin controller and a view.
It should also be mentioned that a bunch of different functionality can be associated with one table.
Actually questions:
1) What is model Active Record'a?

  • a) An entity is an object. It has methods for interacting with other entities, for example $cart→addItem($item) + CRUD methods.
  • b) A set of CRUD methods, essentially a procedural approach?

2) How to create models?
  • a) One model per table
  • b) Each task has its own model, i.e. there can be several models for one table.

PS
I believe that specifically Active Record has nothing to do with it, and this question is also relevant for other ORM patterns

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Novikov, 2017-07-24
@GregIv

Active Record is a pattern, its canonical explanation can be found in Fowler.
design-pattern.ru/patterns/active-record.html
https://www.martinfowler.com/eaaCatalog/activeReco...
Trying to compare AR and models is like comparing warm to soft. Model - the MVC application layer responsible for data processing and business logic. The model can be implemented with a bunch of patterns if required, including AR if you want to store in the database and map the fields of the base to the fields of the object with encapsulation of access to the base.
Imagine an application where, for example, you need to process an image, apply 3 filters, expand it, upload a copy to the cloud, and spit out the second one into the browser. Do you need any logic? Definitely a lot of action. Implemented by controllers? no, the controller just needs to understand what the request is and to whom to delegate it. Views? especially not, it's not about data processing. So models. Well then, sculpt AR (assuming AR=model)? And what does AR have to do with it, there is not even a base in the application :) Model is a much broader concept, and there are immeasurably more ways to organize code on this layer than one pattern.
Actually, the "Active Record model" is true for stupid applications like CRUD without the slightest logic, except for saving data to the database, reading it from there, updating from forms and deleting it. Where all the logic (layer M of the application) is in CRUDs.

D
Dmitry, 2017-07-24
@backender_ru

In Yii approach one table is one AR model.
In general, M is not AR, it is generally the whole business logic of your web application.
You can have two classes, for example, one implements logic in the form of addItem, removeItem, getItems methods, and this class depends on the corresponding AR model.
Keeping all the logic in the controllers is stupid, for example, you will need CLI code for your application that will work with its controllers.
The controller must pass data to the model, receive a response from it, and display it in the View.

A
Alexander Aksentiev, 2017-07-24
@Sanasol

M - model of anything and whatever. It is data and its processing.
Active record is a design pattern and does not seem to apply to models at all, in the sense of indirectly.
Active record itself is generally an antipattern and violates other generally accepted principles such as SOLID.
1) What is the Active Record model?
a) crud is generally a more global thing in active record, and yes, all models are inherited and contain these methods. And other entities have nothing to do with it either.
b) shta?
Active Record is a simple/convenient work with a data warehouse and nothing more.
Everything else is related to the model in MVC as a whole. Data processing, interaction with other things, etc.
2) How to create models?
a) yes
b) Why does one data set need several models if the data is the same? If they are processed differently, then the handlers must be done differently, not the models.
Other ORM patterns are other patterns, and they have nothing to do with active record. a completely different approach to all this.

G
GregIv, 2017-07-24
@GregIv

I will add a link to the answers:
https://habrahabr.ru/post/321050/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question