Will there be Models in Yii3

This may sound like a stupid question but will there still be models (not ActiveRecords) in Yii3?
I’m not sure because yiisoft/validator is now a custom package and you could do validation on your own without the requirement for custom objects anymore. Plus I read Behaviors and all the magic is removed in Yii3.

However I really like yii models because of their possibilities (different scenarios, converting them to arrays / building models from arrays, automatic validation translations, and all this) and I feel like the framework would miss a huge benefit without it. You were able to create really flexible and powerful objects in nearly no time.

Of course I could just copy the entire class hierarchy of Yii2 into a single new Model class and make it compatible with Yii3 again but I guess that’s not really the purpose.

So are there any plans for this?

1 Like

This may sound like a stupid question but will there still be models (not ActiveRecords) in Yii3?

Yes. It is ready: https://github.com/yiisoft/form. Some things that were present in Yii 2 are missing. Some were removed on purpose (such as scenarios), some aren’t re-added yet.

2 Likes

Thank you so much. May I ask why scenarios were removed? This was really really useful for me.
Of course you could just use the conditional validations of return different values, just wanted to know

May I ask why scenarios were removed?

Because in majority of cases a single model was reused in 2-5 forms. That is bad because:

  1. Adjusting validation rule for one form may break another form.
  2. Data isn’t form specific. Often there are extra fields that aren’t used.
  3. There could be logic for a form. If model incorporates logic for multiple forms, it becomes really fat.

The solution here is simple: create separate form model for each form.

3 Likes

In addition, one form == one FormModel gives a much clearer and easier to debug code.

2 Likes

But it requires much more redundant code in my opinion or at least more useless classes that are all the same except they have different validation rules.

I guess I’ll use conditional validations for the different states for my models. Especially when it comes to multistep forms (that are totally dynamic depending on previously entered data) creating 10 models to cover them all would be insane.
I feel the same when different entered values require different validations as well.

For example when an article for an news page is just a draft it doesn’t need all values, just the essentials but when you want to publish it, all are required. All of these would share the same attributes as well as the same attribute labels, functions… the controller wouldn’t know what kind of class should be created unless I move all the logic from the model into the controller.

I would rather call those models instead of forms tho. When passing the data via import script or create a rest api creating a new “form” doesn’t sound that intuitive in my opinion… Maybe I just didn’t get the idea or maybe it’s because most of my models were totally dynamic due to behaviors and the ability for our customers to attach fields how they like in order to add and remove data depending on their needs.

I have to point out that some people (myself) use scenarios without forms. I use scenarios in Yii2 to control afterSave methods and other non-form related conditions, while I mostly use default scenario for form input.

ps “propertys” is spelled properties

pps the cursor and select seem to be missing in this text box

Right at the project development start - maybe. After some time - likely not.

Your use cases are definitely interesting but I think these are better to be solved without scenarios.

Have I understood correctly, that best approach in today’s Yii2 is trying not to use scenarios at all in any part of application and moving those scenario cases to Form level? So, that let say SCENARIO_NEW_USER will be NewUserFrom and SCENARIO_OLD_USER will be OldUserForm?

And not using any behaviors in AR models of Yii2 by using behaviors in Form models, as for example NewUserFrom use “created_at” Behavior and OldUserForm use “modified_at” Behavior?

Is there a list of suggested preparation in today’s Yii2 developments in order to move to Yii3 with less effort? So, we implement Yii3 logic in those areas of Yii2, where it can be implemented (at least to have mindset in right direction)?

1 Like

Not sure it’s officially advertised as “best practice” anywhere but yes, from my experience that doesn’t cause any problems when project grows unlike using scenarios.

Nope. Behaviors in AR models in Yii 2 are fine. Scenarios are fine there as well. Yii 3 is a different story.

2 Likes

They have only validation rules, so no redundant code here, example:

<?php

namespace frontend\models;

use common\models\Reviewer;

class ReviewerInvitationAcceptForm extends Reviewer
{
    public function rules()
    {
        return [
            [['first_name', 'paypal'], 'trim'],
            [['first_name', 'paypal'], 'required'],
            [['paypal'], 'unique'],
        ];
    }

}
1 Like

And my model would then initiate a different class depending on the values and call the rules function? I’m not really sure how that should answer my question to be honest :sweat_smile:

In the end I only load the post values in my model and want to call validate not sure where the different classes come to play.

This is the best one in models in yii3.