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

时间:2016-07-28 15:06:16

标签: methods yii2 edit

让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

2 个答案:

答案 0 :(得分: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]);
} 

答案 1 :(得分:0)

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