Whether in yii2 to create models for uniforms?

Long tormented by the question whether it was necessary to create such a model Singup(like this), if you can use a basic User model:

public function actionSignup()
{
 if (!Yii::$app->user->isGuest) {
 return $this->goHome();
}

 $model = new User(); 
 if ($model->load(Yii::$app->request->post())) {
$model->setPassword($model->password);
 if ($model->save()) {
 if (Yii::$app->getUser()->login($model)) {
 return $this->goHome();
}
}
}

 return $this->render('signup', [
 'model' => $model,
]);
 }


Instead of:

public function actionSignup()
{
 $model = new SignupForm();
 if ($model->load(Yii::$app->request->post())) {
 if ($user = $model->signup()) {
 if (Yii::$app->getUser()->login($user)) {
 return $this->goHome();
}
}
}
 return $this->render('signup', [
 'model' => $model,
]);
 }
June 14th 19 at 18:55
4 answers
June 14th 19 at 18:57
Solution
Forms are better tolerated and preispolitik
Forms may not be tied to the attributes of the model and to better Express the subject area
- Forms are easier to test
- Using forms easier to develop in the team
- Model validation violates the principles of SOLID (AR, in principle, violates, but at least this part can be neutralized)
For basic CRUD operations with minimal business logic can be dispensed with, but if the model is tied a lot of logic and scenarios will be to grow the model will quickly turn into a divine object
- Sculpt all the validation in one form of the model is no better than mold them into Ar-model -> for each case a separate model-form
-------------------
Summary: Use the model validation with scripts until you understand what the desired shape. Use for use, without awareness - is still senselessly and mercilessly
Good evening.
For example some forms, but they all use the same model table in the database. The only difference is that in each form its own set of fields. Solved this problem using scripts.
Or all the same to alter at several models, one model in one form? - Dion77 commented on June 14th 19 at 19:00
First, do not think in terms of active-record model. Where not already mentioned, and the developers admit they made a mistake because of the name of the model class making a snafu. The model is not a single class. and the totality.
>The only difference is that in each form its own set of fields
Try to consider of the forms and fields, and from the business logic - for what purpose do you serve these forms, and which business processes need to happen.

Model forms are used for handling user data and providing additional information when needed. To save to the database was only valid data. One request - one form of model which should return a valid dataset.

> Or all the same to alter at several models, one model in one form?
It makes no sense to redo it until you come understanding what you need to alter. If you do not come understanding on a particular project, then the norms and not have to redo anything. - Brett.Haley commented on June 14th 19 at 19:03
forms used to gather orders for shipping.
The first - return call(name and phone), the second - a short message(name, email and message), and third different parameters of the goods(name, route, and mail is always there).
The project was originally in yii1, all piled up in one heap, a lot of if else to resolve where and what to insert. Broke into several actions with different scenarios to smaller checks and not getting confused. But there was one common. Rather for backend and frontend are two separate and one parent, which contains General data for backend and frontend. In the frontend of the model is the validation in action, what is the script to connect, and I think that is crap. - Dion77 commented on June 14th 19 at 19:06
and the data from all these forms you have stored in one table?
What actions occur after filling each form? Just saved that the user navvodili and all, or they still generate some processes?

If validation deal with the relevant request form and the appropriate services there is no need to produce copies of the AR model. its task is only to store data, and display data.

Although if you have something like a landing page with the order call feedback and a calculator of the goods, and the special development and support of the project are not expected, can not bother. - Brett.Haley commented on June 14th 19 at 19:09
,
Forms can be tied to the attributes of the model and to better Express the subject area

What forms should Express the subject area? Shape this is only the display layer, there may occur only rudimentary data validation that is not tied to the business logic. This can be compared with front forms validation before sending to SPA.
Model validation violates the principles of SOLID

There are different implementations, but in most cases it is recommended(including in the dock yii): the model should include validation of business rules implementing business logic subject area.
AR is just a pattern which combines the business entity and the repository(the object responsible for storing data).

In my opinion it is not necessary to advise something not good. People can come astray. In the forms to keep the business logic is utter nonsense, the form is a representation.
Of course maybe I misunderstood you. - Davonte.Renner commented on June 14th 19 at 19:12
, long time did not come, I'm not talking about storing business logic in the forms, and about the fact that these forms do not always correspond with AR-model = table if it is not simple crud.
yiiframework.ru/forum/viewtopic.php?p=212604#p212604
https://github.com/yiisoft/yii2/commit/35f7c61f776...
>>in the model should include validation of business rules implementing business logic subject area.
But the business model !== AR-model, the model is not a single class. - Brett.Haley commented on June 14th 19 at 19:15
June 14th 19 at 18:59
Models only a thin AR classes with attributes without validation.
Forms of this entry and validation.
Use the principle of SRP, forms should validate the data, the essence of AR is to store just the fields.
June 14th 19 at 19:01
not for "specific purposes" and for forms. Yes, you have.
I do not remember what exactly it was pathetic when even when there is no shape model, but something definitely pathetic.
but if you are all so right, yet limit the User, then the slack if that.
Yes, exactly, for forms. So, I therefore wonder what the pros give individual models under shape. - Dion77 commented on June 14th 19 at 19:04
if you need to the form field to add that will not have to add it to the model=the database=in other places CRUD - Brett.Haley commented on June 14th 19 at 19:07
Yes , but this is again a specific case(specific to my example) - Dion77 commented on June 14th 19 at 19:10
June 14th 19 at 19:03
whether it was necessary to create such a model Singup

The User with a better scenario
$model = new User;
$model->scenario = User::SCENARIO_LOGIN;

$model = new User;
$model->scenario = User::SCENARIO_REGISTER;


www.yiiframework.com/doc-2.0/guide-structure-model...
Scenarios have been used and they seem to me much better to decide this issue, unlike some models under shape, and therefore asked this question - Dion77 commented on June 14th 19 at 19:06
Scenario - the utter dark evil that was invented in Yii - Brett.Haley commented on June 14th 19 at 19:09
arguments - Dion77 commented on June 14th 19 at 19:12
,
the basic template.
Users.
1. Registration.
2. The password reset.
3. The changing role of.
4. The change of status.
5. Edit the profile.
6. Adding/editing user in admin panel.
...
n. ...
Good for each operation to create a separate script.
Here we have 6 scenarios, because you will not allow the user to change the id or role, by pomena data in html. Also in certain scenarios, you may need to perform some operation on one or more fields. Then everything is forgotten, confusion begins, Gorodeya crutches, and the following conver quiet mother previous - Brett.Haley commented on June 14th 19 at 19:15

Find more questions by tags YiiPHP