Yii2:调用未知方法:backend \ controllers \ UserController :: findModel()

问题描述 投票:0回答:2

有Yii2项目创建我自己的博客,我可以查看索引列表来显示数据库中的数据,但是我需要访问foreach并能够访问编辑页面,然后出现问题。这是我在UserController中编辑的代码:

  namespace backend\controllers;

  use yii\web\Controller;
  use common\models\User;
  use Yii;

public function actionIndex(){

    $query = User::find();
    $count = $query->count();
    $pagination = new Pagination(['totalCount'=>$count, 'pageSize'=>5]);
    $results = $query->offset($pagination->offset)->limit($pagination->limit)->all();

    return $this->render('index',['results'=>$results,'pagination'=>$pagination]);
}

  public function actionEdit($id){
    // $id = (int)$id;
    $model = $this->findModel($id);

    if($model->load(Yii::$app->request->post()) && $model->save()){
        return $this->redirect(['index']);
    }

    return $this->render('edit',['model'=>$model]);
} 

这是我的编辑视图的代码:

<div class="inner-container">
    <?=Html::beginForm(['user/edit'],'post',['enctype'=>'multipart/form-data','class'=>'form-horizontal', 'id'=>'addForm'])?>
        <div class="form-group">
            <?=Html::label('Username*:','username',['class'=>'control-label col-sm-2 col-md-1'])?>
            <div class="controls col-sm-10 col-md-11">
                <?=Html::activeInput('text',$model,'username',['class'=>'form-control input'])?>
                <?=Html::error($model,'username',['class'=>'error'])?>
            </div>
        </div>  
        <div class="form-group">
            <?=Html::label('Email*:','email',['class'=>'control-label col-sm-2 col-md-1'])?>
            <div class="controls col-sm-10 col-md-11">
                <?=Html::activeInput('email',$model,'email',['class'=>'form-control input'])?>
                <?=Html::error($model,'email',['class'=>'error'])?>
            </div>  
        </div>
        <div class="form-group">
            <label for="sort_order" class="control-label col-sm-2 col-md-1">Password:</label>           
            <div class="controls col-sm-10 col-md-11">
                <?=Html::activeInput('password',$model,'password',['class'=>'form-control input input-small'])?>
                <?=Html::error($model,'password',['class'=>'error'])?>
            </div>
        </div>

        <div class="form-group">
            <label for="status" class="control-label col-sm-2 col-md-1">Status:</label>         
            <div class="controls col-sm-10 col-md-11">
                <?=Html::activeDropDownList($model,'status',ArrayHelper::map($active,'statusID','statusCN'),['class'=>'form-control width_auto'])?>
            </div>
        </div>
        <div class="form-group">
            <div style="margin-top:10px" class="col-sm-10 col-sm-offset-2 col-md-11 col-md-offset-1">
                <?=Html::submitButton('Submit',['class'=>'btn btn-primary'])?>

                <a class="btn btn-primary" href="<?=Url::to(['index'])?>">Back</a>
            </div>
        </div>
    <?=Html::endForm()?>
</div>

上面的代码正在创建/添加新记录,但编辑项目时出错。

调用未知方法:backend \ controllers \ UserController :: findModel()

我尝试使用以下代码更改为actionEdit方法:

public function actionEdit($id){
//change here
$model = User::findOne($id);

if($model->load(Yii::$app->request->post()) && $model->save()){
    return $this->redirect(['index']);
}

return $this->render('edit',['model'=>$model]);

}

然后它显示:

为foreach()提供的参数无效

不知道我的代码有什么问题。用户模型扩展了ActiveRecord。

部分视图索引:

<tbody>
        <?php foreach($results as $user){?>
        <tr>
        <td class="text-center">
        <input type="checkbox" name="selected[]" value="<?=$user['id']?>">
        </td>
        <td><?=$user['username']?></td>
        <td><?=$user['email']?></td>
        <td><?=$user['login_ip']?></td>
        <td><?=date('Y-m-d',$user['create_date'])?></td>
        <td><?=($user['status'] == 0) ? "NonActived" : "Actived" ?></td>
        <td><a href="<?=Url::to(['edit','id'=>$user['id']])?>" title="Edit" class="data_op data_edit"></a> | <a href="javascript:void(0);" title="Delete" class="data_op data_delete">edit</a></td>
        </tr>
        <?php }?>
</tbody>

上面的编辑链接可以显示为:

http://xx/yii2AdvancedBlog/backend/web/index.php?r=user%2Fedit&id=2

但是一旦点击编辑按钮,它就会显示第一个错误。

<hr>

更新问题


this is add action this is UserController index display enter image description here enter image description here
methods yii2 edit
2个回答
0
投票

好像你说错了动作

在您的代码中

<?=Html::beginForm(['user/add'],'post',['......

这意味着你要调用控制器用户的动作添加..但你正在谈论编辑动作..可能是你需要的

<?=Html::beginForm(['user/edit'],'post',['

对于为foreach()提供的无效参数

确保你的动作有一个正确的$结果,然后在渲染视图中

试试

if (is_array($results) || is_object($results))
{
     foreach ($results as $user) {
      ...
      }
} else {

  var_dump('$result is invalid';
}

那么你似乎没有findModel函数,所以假设你有一个带有和id字段的User类尝试使用这个代码

public function actionEdit($id){
  // $id = (int)$id;
  // $model = $this->findModel($id);
  $model = User::find()->where(['id' => $id])->one();

  if($model->load(Yii::$app->request->post()) && $model->save()){
      return $this->redirect(['index']);
  }

  return $this->render('edit',['model'=>$model]);
} 

0
投票

使用这个$model = User::find()->where(['id' => $id])->one();而不是这个$model = $this->findModel($id)解决了我遇到的类似问题

© www.soinside.com 2019 - 2024. All rights reserved.