Working models with generalization

How i should work with models in generalization relationships. For example:

Model Person could be a Employee or Customer, each of them has some particular attributes, how would the form? A select box change de type of person and load the correct form for type selected?

Any tip?

Are you asking about the models or the forms?

I understand that you need

  1. A Select to choose for example if the model $user->type could be E=Employee or C=Customer

  2. after you select the value you load the form with the field of the relative model choosed

the point 1) i usually use Kartik Select2 like so



use kartik\widgets\Select2;



            $form = ActiveForm::begin([


            echo $form->field($user, 'type')->widget(Select2::classname(), [

                'id' => 'user_type',

                'name' => 'user_type',

                'data' => array('E'=>'Employee','C'=>'Customer'),


                'options' => [

                    'placeholder' => 'Select User Type ...',


                'pluginEvents' => [

                    "change" => "function() {

                    $.post( \"".Yii::$app->urlManager->createUrl('yourcontroller/youraction')."\", {usertype:$(this).val()}, function( data ) {

                        $( \".form2\" ).html( data );







<div class="form2"></div>

the point 2) now you must create a controller something like so:

CONTROLLER yourcontroller

    public function actionYouraction($usertype)


      if ($usertype=="C") {

         $model = new Customer();

         $form = "_form_customer";

      } else {

         $model = new Employee();

         $form = "_form_employee";


            return $this->renderAjax($form, [

                'model' => $model



I hope this can help you.

The forms, Sandark.

It helped a lot, Federico. But don’t solved the problem…

For example, i have the modeling below:

A person could be a Customer or Employee, each have attributes in common with "person". How do work with this way?

I see that a person in the DB teorically could be a Customer AND/OR Employee or not?

Anyway, how do you imagine the form page?

[input name]

[input phone]

[choose Customer/Employee]

if customer [cnpj]

if employee [pf]

something similar?

Exactly, Federico!

Looking at your tables you can do in different ways.

As i suggest for me is the best method, beacuse if you learn this you can re-use many other times.

But, the difference of the choice of Employee or Customer, is only one column: cnpj or cpf to show.

So you can manage this directly with a jquery script.

You pass at your view both Customer and Employee model, of course Person model too. And you can do a form with

[input $person->name]

[input $person->phone]

[choose Customer/Employee] can be a select/radio button group. On change element run your jquery script that show or hide the next 2 input

if customer [cnpj] visible=true else false

if employee [pf] visible = true else false

and then you process all in the Controller/Action after the submit

Thanks, Federico. I will follow your tip