In addition to data verification and basic deletion, what code can model write? To add, delete, and update multiple rows, data preprocessing must be performed based on multiple conditions, perform operations on multiple tables at the same time (there is no such relationship as one-to-many and multiple-to-one). Here there is a large part... in addition to data verification and basic deletion, what code can model write? To add, delete, and update multiple rows, data preprocessing must be performed based on multiple conditions, at the same time, you need to perform operations on multiple tables (there is no such relationship as one-to-many and multiple-to-one). Here there is a large part of code, and it feels great to write it into the controller, sometimes it doesn't work if I write it on the model. Are these all written in helper, or are there other good places to organize them?
Reply content:
In addition to data verification and basic deletion, what code can model write? To add, delete, and update multiple rows, data preprocessing must be performed based on multiple conditions, at the same time, you need to perform operations on multiple tables (there is no such relationship as one-to-many and multiple-to-one). Here there is a large part of code, and it feels great to write it into the controller, sometimes it doesn't work if I write it on the model. Are these all written in helper, or are there other good places to organize them?
By feeling and experience, my answer is purely a piece of cake.
Yii2 does not have any special concept of model, which is rarely mentioned in documents and examples. I feel that they just want to use the ActiveRecord object (or even the form is inherited here, but in fact this is a bad thing, because when multiple tables are linked, the Object Boundary will become blurred. However, after I customized the model layer in a project, the other party refused to pay the project's final payment because the method was not recommended by Yii2.
In my opinion, the ActiveRecord object is a ing of data tables. It should only handle internal tasks, which is undoubtedly much clearer. It is the object ing of the data table, not the model.
However, in many cases, multiple tables must be linked. For example, when creating an order, you must operate the Order details at the same time. Therefore, this logical control should be completed in the model.
In addition, if the Controller and ActiveRecord are completed by Different developers, the model layer is also very important and can be used for coupling between the two parties, because it is quite dangerous to allow direct calls to ActiveRecord In the Controller.
If you think the Model is not enough, you can choose Service, Plugin, Filter, and so on, not just Helper.
Supplement: It seems that questions have not been deducted yet. Haha
In my opinion, the Model is a more complex data Model based on a data table. It often represents developers' understanding and definition of objects. For example, we have an order table and an Order Details table, however, this is not the object-level data, while OrderModel is the declaration of the Order object. An order not only has order information, but also contains order details, and may even be associated with logistics and invoices.
I usually include
Constant definition, if you need to set some specific labels
Transaction processing, if multiple ActiveRecord links are required
Simple CRUD: To isolate ActiveRecord, it means that the Controller does not directly call this type of object, but must encapsulate the method in the model.
Others, I think Helper is mainly used for the front-end, so a common method is not used for the front-end, so it can be placed in the model. In addition, some methods may not be able to determine its ownership, that is, Amodel needs, bmodel is also required and simple. Both are declared, but one is the specific logic and the other is just a reference.
You can use behavior (Behaviors ).
Put ambiguous shared code in behavior.
Similar to Traits.
Or components. Components
Data attributes and behaviors of objects.