V
V
Veronika Stepanovna2017-02-21 17:11:07
Yii
Veronika Stepanovna, 2017-02-21 17:11:07

Where is it more correct to place the logic and work with the database according to mvc?

Now I'm studying the yii2 framework and at the same time writing a website. I have all the logic of work and operations with the database in the model. The controller only requests this information from the model and delegates processing (including logic) to it. Am I doing the right thing according to MVC? Or should the model only have validation rules and CRUD operations without any logic?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mitaichik, 2017-02-21
@mitaichik

On good, in the model there should be only the logic of the entity. Validation is to some extent the logic of the entity. The ActiveRecord pattern also adds work with the database. This is convenient for relatively simple projects, but if you have complex work with the database, then the model class will swell. Therefore, it makes sense to move the database logic from the model class to the appropriate objects. For example, repositories. Another excellent example of the removal of the logic of working with the database is the creation of a class of its own ActiveQuery with its own scopes. Business \ Operational logic - to the appropriate services. Validation logic - in your validators. Well, etc.

S
Sergey, 2017-02-21
@sscrm

That's right, the business logic should be in the models. I don’t know how in yii, but in symfony the models are divided into entities and repositories, and all business logic is placed in the repositories. There is such an anti-pattern "thick controller", this is when they try to shove business logic into the controller, you shouldn't do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question