how to generating a dropdown list in yii2

耗尽温柔 提交于 2020-01-06 23:46:17

问题


I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?

my code is :

<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata);  ?>

but I have a problem in line of using ArrayHelper
the problem is: PHP Notice – yii\base\ErrorException Array to string conversion.......! I tested the below code :

 $listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');     

but it dos not solved and has the same error!

whats the problem? can somebody help me?


回答1:


You are trying to echo an array, change <?= to <?php in:

<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>



回答2:


Try like this

   <?php
     use yii\helpers\ArrayHelper;
     use app\models\Product;
    ?>

   <?= $form->field($model, 'parent_id')->dropDownList(
        ArrayHelper::map(Product::find()->all(),'id','name'),
        ['prompt'=>'Select ']) 
    ?>


来源:https://stackoverflow.com/questions/28092502/how-to-generating-a-dropdown-list-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!