S
S
santaatnas2016-12-10 17:16:25
Yii
santaatnas, 2016-12-10 17:16:25

Where to store custom business logic classes in Yii2 and is it worth storing database queries in its model?

Hello everyone, please help me with this issue. Yii2 advanced, let's say there is a table model, located in the frontend/models/Review.php folder, it describes the table of the same name in the "review" database and is inherited from the ActiveRecord class, the table contains a set of fields and we need to get, say, all records by date. We create a public static function getReviewByData( $data )... method that returns our posts to us. Is it right to make the model thick and store all the logic there, all methods for getting and writing data, and then call them in the controller, or is it worth creating a separate class for CRUD manipulations and calling methods from it, or in general (I saw examples when creating queries to the database in view, which I think is fundamentally wrong). And another question, where in the advanced template to store business logic (its classes), what are the correct practices (of course, to have the possibility of autoloading classes using standard yii2 tools). Let's say I want to implement some pattern, I should store the whole pattern in any one namespace or different classes and interfaces in different namespaces... Thanks in advance for your answer.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2016-12-12
@webinar

the table contains a set of fields and we need to get, say, all records by date. We create a public static function getReviewByData( $data )... method that returns our posts to us. Is it right to make the model thick

Here is what the docs say about this:
www.yiiframework.com/doc-2.0/guide-db-active-recor...
namely, they advise creating a ModelQuery class inherited from ActiveQuery while having a Model class inherited from ActiveRecord and, as a rule, here ModelSearch is also added. In the docks, everything is in the model, but inside the model I create query and search subfolders.

E
Eugene, 2016-12-10
@Nc_Soft

Usually they make controllers and models thin, and all the noodles are fenced in separate classes-repositories.
See repository pattern

M
Maxim Fedorov, 2016-12-10
@qonand

Create a public static function getReviewByData( $data )... method that returns our posts to us

Well, in general, it is desirable to implement the formation of queries in a separate class, inherited for example from ActiveQuery
Don't confuse model in MVC and ActiveRecord in YII. ActiveRecord is just a pattern that implements a certain front of work with a database table, while the model must contain the business logic of the application. Accordingly, if you have ActiveRecord - Review, then it is better not to do business logic in it, but to implement it in separate classes with which the controller will interact
Yes, where you want, it all depends on the architecture that you provide in the project. Yii does not impose any restrictions on this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question