add custom field from database

how to add custom fields in Yii-2.

i have table user ,there i have fields and creating form with gii but later user can add custom fields so how i create those fields in view without generator.

They will automatically be available to the model if it is bound directly to the table using ActiveRecord, you can then modify the view and add the additional fields in the same way as the original ones using $form->field() etc.

When you say custom fields, do you mean just new fields added to the table in the future?

i need to generate automatically in view

First question is how you know which fields are custom. You could do this with a separate table and have a relationship in your user model or you might have some other way of selecting them. Once you have created your collection of data properties, you can use foreach in the view to write them out to the page.

Another way would be to have them in the user table with perhaps a prefix like custom_field1 and then you could access them inside a foreach to check for each property and say, if ( startsWith($property->name, "custom_" ) $form->field…

With the second way, they will save automatically because the variables will exist in the normal post data and will be copied across like normal.

I’m sorry but I don’t know that many details about the easiest way to get the model properties and stuff but it will all be in ActiveRecord class. Alternatively you can use a database query directly in the model to get what you need.

basic rule - to alter generated code you must regenerate the code with tool it was generated

gii gives ability of one-way generation, "do and forget". You regenerate same stuff with new column(any manual changs will be lost) or you alter generated code manually (as you probably should)

gii is nothing more than just a kids tool until you extend it with your templates. write code with your hands