我想使用本教程創建一個小的CRUD應用程序: http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2/Yii2/PostgreSQL的 - 調用未知的方法:警予 DB ActiveQuery ::表格名稱()
一切,但更新和刪除功能工作。當我點擊到「更新」,我收到以下錯誤:
Calling unknown method: yii\db\ActiveQuery::formName()
當我點擊到「刪除」,我得到這個錯誤:
Method Not Allowed. This url can only handle the following request methods: POST.
這些都是我SiteController功能:
public function actionDelete($id=NULL)
{
$model = $this->loadModel($id);
if (!($model->delete()))
{
Yii::$app->session->setFlash('error', 'Unable to delete model');
}
$this->redirect(\Yii::$app->urlManager->createUrl('site/index'));
}
public function actionSave($id=NULL)
{
if ($id == NULL)
{
$model = new Crud;
} else {
$model = $this->loadModel($id);
}
if(isset($_POST['Crud']))
{
$model->load($_POST);
if ($model->save())
{
Yii::$app->session->setFlash('success', 'Model has been saved');
$this->redirect(['save', 'id' => $model->id]);
} else {
Yii::$app->session->setFlash('error', 'Model could not be saved');
}
}
echo $this->render('save',['model' => $model]);
}
private function loadModel($id)
{
$model = Crud::find($id);
if ($model == NULL)
{
throw new HttpException(404, 'Model not found');
}
return $model;
}
有沒有人知道我在做什麼錯? 在此先感謝!
編輯:完整錯誤消息:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request: site/error' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Controller.php:122
Stack trace:
#0 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('error', Array)
#1 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\web\ErrorHandler.php(79): yii\base\Module->runAction('site/error')
#2 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\ErrorHandler.php(95): yii\web\ErrorHandler->renderException(Object(yii\web\MethodNotAllowedHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\MethodNotAllowedHttpException))
#4 {main}
Previous exception:
exception 'yii\web\MethodNotAllowedHttpException' with message 'Method Not Allowed. This url can only handle the following request methods: POST.' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\filters\VerbFilter.php:105
Stack trace:
#0 [internal function]: yii\filters\VerbFilter->beforeAction(Object(yii\base\ActionEvent))
#1 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Component.php(538): call_user_func(Array, Object(yii\base\ActionEvent))
#2 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Controller.php(259): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#3 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#4 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#5 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('delete', Array)
#6 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('site/delete', Array)
#7 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\basic2\web\index.php(12): yii\base\Application->run()
#9 {main}
EDIT2: 我的視圖(save.php)包含以下內容:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'name')->textInput(array('class' => 'form-control')); ?>
<?php echo $form->field($model, 'alter')->textInput(array('class' => 'form-control')); ?>
<?php echo Html::submitButton('Senden', array('class' => 'btn btn-primary pull-right')); ?>
<?php ActiveForm::end(); ?>
我的index.php視圖包含以下:
<?php
use yii\helpers\Html;
$this->title = 'CRUD';
?>
<?php echo Html::a('Neuen Eintrag erstellen', array('site/save'), array('class' => 'btn btn-primary pull-right')); ?>
<div class="clearfix"></div>
<hr />
<table class="table table-striped table-hover">
<tr>
<td>ID</td>
<td>Name</td>
<td>Alter</td>
<td> </td>
</tr>
<?php foreach ($models as $post): ?>
<tr>
<td><?php echo Html::a($post->id, array('site/save', 'id'=>$post->id)); ?></td>
<td><?php echo Html::a($post->name, array('site/save', 'id'=>$post->id)); ?></td>
<td><?php echo Html::a($post->alter, array('site/save', 'id'=>$post->id)); ?></td>
<td>
<?php echo Html::a('Update', array('site/save', 'id'=>$post->id)); ?>
<?php echo Html::a('Delete', array('site/delete', 'id'=>$post->id)); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
謝謝消息「未找到方法」也不再出現在PhpStorm,但是從上面的錯誤仍然出現。我在我的第一篇文章中添加了完整的錯誤信息。 – Gummikuchen 2014-11-21 12:21:53
@Gummikuchen那是因爲你沒有'site'控制器和'error'動作。確保那些存在 – 2014-11-21 12:36:50
再次感謝!第一個錯誤是固定的,第二個錯誤仍然發生: 「Method Not Allowed。此URL只能處理以下請求方法:POST。」 – Gummikuchen 2014-11-21 12:56:18