简体   繁体   中英

Trying to get dropdown menu with values from database but getting array to string conversion error (YII2)

This is my code:

<?= 
    $rates= ApplicantRating::find()->all();     
    $listData= \yii\helpers\ArrayHelper::map($rates,'id','name'); 
    
    echo $form->field($model,'name')->dropDownList($listData,['prompt'=>'Select...']);
?>

I have a:

PHP Notice – yii\base\ErrorException Array to string conversion error

and I cannot understand why. It is highlighting the line $rates= ApplicantRating::find()->all(); as the problem. Any suggestions pls?

Thanks a lot.

You need to change <?= to <?php like follows:

<?php 
    $rates= ApplicantRating::find()->all();     
    $listData= \yii\helpers\ArrayHelper::map($rates,'id','name'); 
    
    echo $form->field($model,'name')->dropDownList($listData,['prompt'=>'Select...']);
?>

<?= [sentence here] is the same as <?php echo [sentence here] So, what happens is php tries to echo $rates .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM