Yii2 / PostgreSQL - 调用未知方法:yii \ db \ ActiveQuery :: formName()

时间:2014-11-21 08:32:06

标签: php postgresql methods yii2 crud

我正在尝试使用本教程创建一个小型CRUD应用程序: http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2/

除了更新和删除功能之外的所有功能都正常。当我点击"更新"时,我收到以下错误:

  

调用未知方法:yii \ db \ ActiveQuery :: formName()

当我点击"删除"时,我收到此错误:

  

不允许的方法。此网址只能处理以下请求   方法: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>

1 个答案:

答案 0 :(得分:1)

如果要删除记录,则必须按如下方式获取记录:

$model = Crud::find($id)->one();

所以:

$model = Crud::find($id)->one()->delete();

或者您可以使用此:

protected function findModel($id)
{
    if (($model = Crud::findOne($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

然后在行动:

$model = $this->findModel($id);

if (!($model->delete()))
{
    Yii::$app->session->setFlash('error', 'Unable to delete model');
}

$this->redirect(\Yii::$app->urlManager->createUrl('site/index'));
  

不允许的方法。此URL只能处理以下请求方法:POST。

以上错误通常与请求类型相关,只允许POST。要解决这个问题,你必须告诉Yii2如下处理它:

<?php echo Html::a('Delete', array('site/delete', 'id'=>$post->id),array('data-method'=>'post')); ?>