Yii2 external keyword dropdown

I am trying to display a dropdown with a category in the yii2 structure.

The tables in my database are configured using a foreign key and the Model and Crud generator are used to generate the code.

I am trying to change the code now to change the text box in the drop-down list with the values ​​from the category table.

<?php $categoryArray = ArrayHelper::map(\app\models\Category::find()->orderBy('name')->all(), 'id', 'name') ?>
<?= $form->field($model, 'category_id')->dropDownList($categoryArray, ['prompt' => '---- Select category ----'])->label('category') ?>

This returns with the error "2.yii \ base \ ErrorHandler :: handleFatalError ()"

Most of the related messages with my problem relate to version 1 of the structure, but cannot find a good example of how to do this with version 2.

+4
source share
3 answers

, , "Class ArrayHelper not Found" , :

use yii\helpers\ArrayHelper;
+1
 use yii\helpers\ArrayHelper; 

 use backend\models\Model_name; 

 <?= $form->field($model, 'Field_id')->dropDownList(
            ArrayHelper::map(<Model_name>::find()->all(),'Field_id','Field_name'),
            ['prompt'=>'Select XYZ']
       )?> 
+3

->asArray() :

$categoryArray = ArrayHelper::map(\app\models\Category::find()->orderBy('name')->asArray()->all(), 'id', 'name');
0
source

Source: https://habr.com/ru/post/1589409/


All Articles