dropdownlist - Unknown Property – yii\base\UnknownPropertyException

Hi,

Need help to display list in dropdown list. I’m currently getting the following error:

[b]Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\school\School::getStateList[/b]

Code in \advanced\backend\models\school\School.php





class School extends \yii\db\ActiveRecord

{

public function getStateList() 

	{ 

        $models = State::find()->asArray()->all();

        return ArrayHelper::map($models,'state_id','name');

    }

}




Code in \advanced\backend\views\school\_form.php




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\helpers\ArrayHelper;


<div class="school-form">


    <?php $form = ActiveForm::begin(); ?>


	<?= $form->field($model, 'state_id')->dropDownList($model->getStateList) ?>  

	


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>




Thank you in advance.

In your School model, you must set getStateList() method is static:


public static function getStateList() 

{ 

    $models = State::find()->asArray()->all();

    return ArrayHelper::map($models,'state_id','name');

}

In \advanced\backend\views\school\_form.php, replace:


<?= $form->field($model, 'state_id')->dropDownList($model->getStateList) ?>

with:


<?= $form->field($model, 'state_id')->dropDownList(School::getStateList()) ?>

Thanks NamLe for your reply.

I’ve changed as per your instructions. I’ve the following error now.

PHP Fatal Error – yii\base\ErrorException

Class ‘School’ not found

You must add this line at the top of _form.php


use app\models\school\School;

New error:

PHP Fatal Error – yii\base\ErrorException

Class ‘app\models\school\State’ not found

Hi NamLe,

Thanks. Your code works.

The subsequent error was caused due to wrong configuration of my namespaces.