Search code examples
phpyiiyii2

Yii2 Php-Call to a member function getAttributeLabel() on array


I have 3 views. The create and update view are the same. In the _form.php view file. I have this:

 <?php
  use yii\bootstrap\ActiveForm;
 ?>

 <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($model, 'name') ?>
        <?= $form->field($model, 'adr_country_id') ->dropDownList($cities) ?>
<?php ActiveForm::end()?>

In the create and update view:

<?= $this->render('_form', ['model' => $model, cities' => $cities ]); ?>

. I can create new record but cant update it. I got the error in the title. This is my controller for update. What might be the problem here? Thanks in advance

public function actionUpdate($id) {

$model = ArrayHelper::map(AdrCountry::find()->all(),'id','name');
$cities = AdrCity::findOne($id);

if ($cities->load(Yii::$app->request->post()) && $cities->validate())
{
    $cities->save();
    Yii::$app->session->setFlash('success', ' updated');
    return $this->redirect(['index']);
}
return $this->render('update',[
    'cities' => $cities,
    'model' => $model
]); }

Solution

  • Update you controller's action

    public function actionUpdate($id) {
    
    $cities = ArrayHelper::map(AdrCountry::find()->all(),'id','name');
    $model = AdrCity::findOne($id);
    
    if ($model->load(Yii::$app->request->post()) && $model->validate())
    {
        $model->save();
        Yii::$app->session->setFlash('success', ' updated');
        return $this->redirect(['index']);
    }
    return $this->render('update',[
        'cities' => $cities,
        'model' => $model
    ]); }