Yii2 dropdownlist from select distict

Hello…

I’m newbie in Yii2. I’ve problem to create dropdownlist, found error “Call to a member function field() on a non-object” . this my models and view

Models :

Public static function getPeriod($Lastyear)


{


	 $command = Yii::$app->db->createCommand("SELECT trans_date,DATE_FORMAT(trans_date,'%d-%m-%Y')AS period FROM transaction WHERE DATE_FORMAT(trans_date,'%Y')='".$Lastyear['year']."' AND STATUS=1 ORDER BY trans_date DESC ");





		$rRows = $command->queryAll();





		//print_r($rRows );


		//die;


		return $rRows ;


}

View :

<?php

$Lastyear = Mymodels::getLastYear();

$listData = Mymodels::getPeriod($Lastyear);

?>

<?= $form->field($model, ‘period’)->dropDownList($listData , [‘prompt’=>‘Choose…’]); ?> //<-- here i get the error “Call to a member function field() on a non-object”

What’s wrong in my code? Please help me…

Thanks

You can try in this way:

Be sure $listData is a simple array, then you can use ArrayHelper::map()


use yii\helpers\ArrayHelper;




<?= $form->field($model, 'period')->dropDownList(ArrayHelper::map($listData, 'key', 'value'), ['prompt'=>'Choose...']); ?>