0

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

1
  • You don't need ::find()->all, because your variable $sds must return array with your data. Commented Jul 6, 2022 at 7:11

1 Answer 1

1

You are not using Model/Class. So, in ArrayHelper

return ArrayHelper::map($sds, 'SalesmanCode', function($sds) {
    return $sds['SalesmanCode'].' - '.$sds['SalesmanNameFull'];
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.