gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:29:29 25 4
gpt4 key购买 nike

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

除了更新和删除功能之外的所有功能都可以使用。当我点击“更新”时,出现以下错误:

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}

编辑2:我的 View (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 View 包含以下内容:

<?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>

最佳答案

当你想删除一条记录时,你必须像下面这样获取它:

$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'));

Method Not Allowed. This url can only handle the following request methods: POST.

以上错误一般与请求类型有关,只允许POST。要解决这个问题,您必须告诉 Yii2 像下面这样处理它:

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

关于php - Yii2/PostgreSQL - 调用未知方法:yii\db\ActiveQuery::formName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27057075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com