Search code examples
yii2dropdownlistfor

Yii2 DropdownList on relationated tables


i have 3 tables

Events *id *idteacher

Teacher *id *person

Person *id *name1 *name2 *lastname1 *lastname2

How can i make a Dropdownlist on Events Form that shows the fullname of person but save the idteacher???

<?= $form->field($model, 'idexpositor')->DropdownList( 
        ArrayHelper::map(TblExpositor::find()->all(),'id','idpersona'),
        ['prompt'=>'Seleccione el nombre del Expositor']

);?>


Solution

  • You can use a proper query .. like this (adapt the field and column name to your need)

    <?= $form->field($model, 'your_field_in_model')->DropdownList( 
            ArrayHelper::map(TblExpositor::find()->
                select('t.id as id,  concat(p.nama1, p.name2, p.lastname1, p.lastname2) as name')->
                from('Teacher t, Person p')->
                where('t.personeid = p.id')->all(),'id','name'),
            ['prompt'=>'Seleccione el nombre del Expositor']
    );?>