Saving some columns but ignoring others

Hi,

There’s a thing about Yii which has been bugging me for a while. I’ve tried here and on Google and can’t find an answer. Can anyone help with this:

SCENARIO 1:

Suppose you have database table called users with the following fields:

id (int)

name (varchar)

email (varchar)

picture (varchar)

Let’s imagine you want a form that allows the user to CRUD their name and email address. HOWEVER, let’s also imagine that you want an entirely separate page for (optionally!) uploading an image for the user.

The question is, how can we get our validation rules to completely ignore ‘picture’ on the page with the form but - alternatively - ignore everything except ‘picture’ on the imagine uploading page?


Here is the same question, rephrased and with a slightly different scenario - just to make what I’m asking clearer:

SCENARIO 2.

Imagine you are building a website for an insurance company. Visitors who want insurance need to fill out lots and lots of fields. To make life easier for customers, the insurance company want to break everything down. So, even though everything will be saved onto one database table - the insurance company want the user to go through five separate, smaller forms.

Again, the question is - how can you modify your model so that this can be achieved?

Thanks in advance of any help.

You can use ‘scenarios’ of the model for it.

http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

It enables for you to ignore certain attributes for a specified scenario.

For larger and/or more complex use cases like your scenario #2, I would rather consider creating a dedicated model for each form, extending from the base model.

Thank you very much. I had a feeling it might be to do with scenarios.

You’re awesome. Have a great week Cheers!

You’re welcome, and have a nice week, too. :)