Yii Framework Forum

Yii2 Dropdownlist using parameter


(Folumikeidowu) #1

I have these 3 Model Classes

ClassMaster
public function attributeLabels()
{
return [
‘class_id’ => Yii::t(‘aca’, ‘ID’),
‘class_name’ => Yii::t(‘aca’, ‘Class Name’),
];
}

Subjects
public function attributeLabels()
{
return [
‘subject_id’ => Yii::t(‘aca’, ‘ID’),
‘subject_name’ => Yii::t(‘aca’, ‘Subject Name’),
];
}

ClassSubjects
public function attributeLabels()
{
return [
‘class_subject_id’ => Yii::t(‘aca’, ‘ID’),
‘class_subject_subject_id’ => Yii::t(‘aca’, ‘Subject’),
‘class_subject_class_id’ => Yii::t(‘aca’, ‘Class’),
];
}

    <div class="col-sm-6">
<?= $form->field($model, 'student_subject_id',['inputOptions'=>[ 'class'=>'form-control'] ])->dropDownList(ArrayHelper::map(app\models\Subjects::find()->all(),'subject_id','subject_name'),['prompt' => Yii::t('app', '-- Select Country --')]);
?>
</div>

I have a parament as $classid. So, I want a Droupdown list that will load all the subjects (subject_id and subject_name) base on the parameter. That is where $classid = class_subject_class_id in ClassSubjects

How do I achieve this


(Folumikeidowu) #2

Problem solved. I did it as shown below:

                    <?= $form->field($model, 'exam_group_subject_id')->widget(Select2::classname(), [
                'data' => ArrayHelper::map(AcaSubjects::findBySql("SELECT aca_subjects.subject_id,aca_subjects.subject_name FROM `aca_subjects` "
                        . "INNER JOIN `aca_class_subjects` ON `aca_class_subjects`.`class_subject_subject_id` = `aca_subjects`.`subject_id` "
                        . "INNER JOIN `aca_class_master` ON `aca_class_master`.`class_id` = `aca_class_subjects`.`class_subject_class_id` "
                        . "WHERE aca_class_subjects.class_subject_class_id = $classids and aca_subjects.is_status= 0")->orderBy('subject_id')->asArray()->all(), 'subject_id', 'subject_name'),
                    'language' => 'en',
                    'options' => ['placeholder' => '--- Select Subject ---'],

                 //   'disabled'=>'true',
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                ]); ?> 

Thanks to everyone.