How to load selected values ​​in multiple drop-down lists in update view in yii?

I am new to yii. I have a problem retrieving selected values ​​from a drop-down list when updating a record.
I have a drop down list with multiple user email choices.
When added, it works fine, it allows me to select multiple values ​​and can insert selected identifier values, separated by commas in the database.
But the problem is that when I want to update a record, it only displays 1 selected record.

Here is my code:

in file :

<div class="controls">
    <?php echo $form->dropDownList($model, 'uid', $allUsers, array('class' => 'select2-me input-sel_large', 'multiple' => 'true', 'options' => array('' => array('selected' => true)))); ?>
        <?php echo $form->error($model, 'uid') ?>
</div>

in the controller file :

$model = new User;
$allUsers = $model->getAllUsers();

in the Model file :

public function getAllUsers() {
        $arr = Yii::app()->db->createCommand()
        ->select('userEmail,pkUserID')
        ->from('tbl_user')
        ->queryAll();
        $users = array();
        if (is_array($arr)) {
            foreach ($arr as $value) {
                $users[$value['pkUserID']] = $value['userEmail'];
            }
        }
        return $users; 
    }

Can anyone help?

+4
1

. , .

class YourForm extends CFormModel
{
    public $uid = array(); // selected pkUserID's
}

//in action
$yourForm = new YourForm();
$yourForm->uid = array(1,2,3); // for example selected users with pk 1,2,3

$this->render('your_view', array('yourForm'=>$yourForm));

//view
/** @var CActiveForm $form */
/** @var YourForm $yourForm */
echo $form->dropDownList(
    $yourForm,
    'uid',
    CHtml::listData(User::model()->findAll(array('order' => 'userEmail ASC')), 'pkUserID', 'userEmail'),
    array('empty' => '', 'multiple'=>true))
   )
+2

All Articles