Mohibul Hasan Rana
Mohibul Hasan Rana

Reputation: 347

How to show dropdown list in YII1 with condition?

I have two tables. One is "user".In user table...

'id person type_id'

'1    X    1'

'2    y    2'

'3    z    3'

Another is "type".

'id typename'

'1  a'

'2  b'

'3  c'

And i have a form where i want to show a dropdown list of person those typename are "a" and "b". My form is ..

<div class="row">
    <?php 


        echo $form->dropDownListGroup(
            $User,
            'supervisor',
            array(
                'wrapperHtmlOptions' => array(
                    'class' => 'col-sm-5',
                ),
                'widgetOptions' => array(
                    'data' => CHtml::listData(User::model()->findAll(), 'id', 'person'),
                    'htmlOptions' => array('prompt'=>'Select'),
                )

            )
        ); ?>
</div>

Upvotes: 0

Views: 925

Answers (1)

Konstantin
Konstantin

Reputation: 566

...
'data' => CHtml::listData(User::getDropDownListData(), 'id', 'person'),
...

class User extends CActiveRecord {
...
public static function getDropDownListData() {

    $types = Type::model()->findAllByAttributes([
        'name' => ['a', 'b'],
    ]);
    $typeIds = array_map(function($item){ return $item->id;}, $types)
    $c = new CDbCritedria();
    $c->compare('type', $typeIds);
    return self::model()->findAll($c);
}
...
}

Upvotes: 1

Related Questions