Yii2:调用未知方法:backend\controllers\UserController::findModel()
Yii2: Calling unknown method: backend\controllers\UserController::findModel()
有 Yii2 项目来创建自己的博客,我可以查看索引列表以显示数据库中的数据,但我需要访问 foreach 并能够访问编辑页面,然后问题就出来了。
这是我在 UserController:
中用于 edit 的代码
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>
上面的代码正在 create/add 一条新记录,但在编辑该项目时出错。
Calling unknown method: 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]);
}
然后显示:
Invalid argument supplied for 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>
以上编辑link可以显示为:
http://xx/yii2AdvancedBlog/backend/web/index.php?r=user%2Fedit&id=2
但是一旦点击编辑按钮,就会显示第一个错误。
<hr>
更新问题
你好像打错了动作
在你的代码中你有
<?=Html::beginForm(['user/add'],'post',['......
这意味着您将操作称为添加控制器用户..但您正在谈论编辑操作..可能是您需要
<?=Html::beginForm(['user/edit'],'post',['
为 foreach() 提供的参数无效
确保您在操作中有正确的 $result,然后在呈现的视图中有正确的 $result
尝试测试
if (is_array($results) || is_object($results))
{
foreach ($results as $user) {
...
}
} else {
var_dump('$result is invalid';
}
那么你似乎没有 findModel 函数所以假设你有一个用户 class 和 id 字段尝试使用此代码
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]);
}
使用这个$model = User::find()->where(['id' => $id])->one();
而不是这个$model = $this->findModel($id)
解决了我遇到的类似问题
有 Yii2 项目来创建自己的博客,我可以查看索引列表以显示数据库中的数据,但我需要访问 foreach 并能够访问编辑页面,然后问题就出来了。 这是我在 UserController:
中用于 edit 的代码 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>
上面的代码正在 create/add 一条新记录,但在编辑该项目时出错。
Calling unknown method: 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]);
}
然后显示:
Invalid argument supplied for 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>
以上编辑link可以显示为:
http://xx/yii2AdvancedBlog/backend/web/index.php?r=user%2Fedit&id=2
但是一旦点击编辑按钮,就会显示第一个错误。
<hr>
更新问题
你好像打错了动作
在你的代码中你有
<?=Html::beginForm(['user/add'],'post',['......
这意味着您将操作称为添加控制器用户..但您正在谈论编辑操作..可能是您需要
<?=Html::beginForm(['user/edit'],'post',['
为 foreach() 提供的参数无效
确保您在操作中有正确的 $result,然后在呈现的视图中有正确的 $result
尝试测试
if (is_array($results) || is_object($results))
{
foreach ($results as $user) {
...
}
} else {
var_dump('$result is invalid';
}
那么你似乎没有 findModel 函数所以假设你有一个用户 class 和 id 字段尝试使用此代码
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]);
}
使用这个$model = User::find()->where(['id' => $id])->one();
而不是这个$model = $this->findModel($id)
解决了我遇到的类似问题