PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

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

 February 26, 2022     php, yii, yii2     No comments   

Issue

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
]); }


Answered By - ExploitFate
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing