Yii 2.0 dropDownList未填充数据库数据


Yii 2.0 dropDownList not populated with database data

试图在我的视图中创建一个下拉列表,该列表由名为"Category"的数据库表中的数据填充。然而,这不起作用,也没有什么东西被填充。

控制器''机箱控制器:

public function actionIndex()
{
    $searchModel = new CaseSearch();  
    $allCategory = new Category;
    $allCategory = Category::findAllCategories();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $post_obj = Yii::$app->request->post();
    $category=1;
    if(isset($post_obj['CaseSearch']['category'])){
       $category = $post_obj['CaseSearch'];
       $dataProvider = $searchModel->searchByCategory($category);   
    }
    $searchModel->category = $category;
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'post' => $post_obj,
        'allCategory' => $allCategory
    ]);
}

型号''类别:

class Category extends 'yii'db'ActiveRecord
{
public $id;
public $name;
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'Category';
}
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id'], 'integer'],
        [['name'], 'required'],
        //[['LastEdited', 'Published'], 'safe'],
        [['name'], 'string', 'max' => 45]
    ];
}
public static function findAllCategories()
{
    Category::find()->indexBy('id')->all();
    return self::find();

}

视图:

echo $form->field($searchModel, 'newcategory')
    ->dropDownList(
        $allCategory           // Flat array ('id'=>'label')
);

知道为什么不起作用吗?

将这些更改为行

$allCategory = new Category;
$allCategory = Category::findAllCategories();

至:

$allCategory = Category::find()->all();

echo $form->field($searchModel, 'newcategory')
->dropDownList(
    $allCategory           // Flat array ('id'=>'label')
);

至:

echo $form->field($searchModel, 'newcategory')
          ->dropDownList(
              ArrayHelper::map($allCategory, 'id', 'name')
            )
?>