I am working on Yii2
. I am using mysql
and mssql
databases. The mssql
is on a remote site and I am able to access it. Now I am trying to add a dropdown list.
Controller
public function actionCreate()
{
$model = new AllowArea();
$sds = Yii::$app->sds->createCommand("Select * from Area")->queryAll();// mssql Database
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'sds' => $sds
]);
}
View
<?= $form->field($model, 'salesman_code')->dropDownList(\common\models\AllowArea::toArrayList(), ['prompt' => 'Select a Booker']) ?>
Model
In my model, I have a function
public static function toArrayList(){
$sds = Yii::$app->sds->createCommand("Select * from Salesmen")->queryAll();
return ArrayHelper::map($sds::find()->all(),'SalesmanCode',function($sds, $defaultValue){
return $sds['SalesmanCode'].' - '.$sds['SalesmanNameFull'];
});
}
Previously I was using self
in place of $sds
. With $sds
I am getting error
Class name must be a valid object or a string
Any help would be highly appreciated
CodePudding user response:
You are not using Model/Class
. So, in ArrayHelper
return ArrayHelper::map($sds, 'SalesmanCode', function($sds) {
return $sds['SalesmanCode'].' - '.$sds['SalesmanNameFull'];
});