Php 易能';t保存数据-未知方法保存()
您好,我是Yii框架的新手,我正在尝试将数据保存到数据库中。不行,我不知道哪里出了问题 控制器:Php 易能';t保存数据-未知方法保存(),php,mysql,yii,yii2-advanced-app,Php,Mysql,Yii,Yii2 Advanced App,您好,我是Yii框架的新手,我正在尝试将数据保存到数据库中。不行,我不知道哪里出了问题 控制器: namespace app\controllers; use app\models\Client; use Yii; use yii\web\Controller; class ClientController extends Controller { /** * Displays Client_Register. * * @return string
namespace app\controllers;
use app\models\Client;
use Yii;
use yii\web\Controller;
class ClientController extends Controller {
/**
* Displays Client_Register.
*
* @return string
*/
public function actionAdd() {
$model = new Client();
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
return $this->refresh();
}
}
return $this->render('add', ['model' => $model,]);
}
}
视图:
我已经用迁移创建了数据库。但我不知道为什么会发生这种错误。我是否应该在模型中包含一些保存方法,或者如何解决此问题。我也看了其他例子。它们与我的代码相同。您知道问题出在哪里吗?您的
客户端
类扩展了模型
,该类不支持在数据库中保存数据,因此未定义save()
方法。如果要使用数据库记录,您的模型应扩展:
您的
客户端
类扩展了模型
,它不支持在数据库中保存数据,因此未定义save()
方法。如果要使用数据库记录,您的模型应扩展:
你有错误吗?尝试var_dump(Yii::$app->request->post())。模型中的PS尝试删除您定义的变量,例如public$id应覆盖属性$id是否有错误?尝试var_dump(Yii::$app->request->post())。模型中的PS尝试删除您定义的变量,例如public$id应覆盖属性$id如何在项目中包括ActiveRecord?因为我发现了这个错误,所以ActiveRecord未找到。请尝试使用FQN:
类客户端扩展\yii\db\ActiveRecord
。如果您从yii开始,我还建议使用Gii生成模型-它将为您生成基本模型:我如何将ActiveRecord包含在我的项目中?由于我遇到此错误,ActiveRecord未找到请尝试使用FQN:类客户端扩展\yii\db\ActiveRecord
。如果您从yii开始,我还建议使用Gii生成模型-它将为您生成基本模型:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'lastname') ?>
<?= $form->field($model, 'birthday') ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary', 'name' => 'add-button']) ?>
</div>
<?php ActiveForm::end(); ?>
namespace app\models;
use yii\base\Model;
/**
* Client is the model behind the client form.
*/
class Client extends Model {
public $id;
public $name;
public $lastname;
public $birthday;
public static function tableName() {
return 'clients';
}
/**
* @return array the validation rules.
*/
public function rules() {
return [
[['name', 'lastname', 'birthday',], 'required'],
];
}
public function attributeLabels() {
return [
'id' => 'Id',
'name' => 'Name',
'lastname' => 'Last Name',
];
}
}
class Client extends ActiveRecord {
public static function tableName() {
return 'clients';
}
public function rules() {
return [
[['name', 'lastname', 'birthday'], 'required'],
];
}
public function attributeLabels() {
return [
'id' => 'Id',
'name' => 'Name',
'lastname' => 'Last Name',
];
}
}