Yii 无法保存数据 - 未知方法 save()
Yii can't save data - unknown method save()
您好,我是 Yii 框架的新手,我正在尝试将数据保存到数据库。不会用不知道是哪里的问题
控制器:
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,]);
}
}
查看:
<?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',
];
}
}
我已经用迁移创建了数据库。但我不明白为什么会发生这个错误。我应该在模型中包含一些保存方法还是如何解决这个问题。我也看了其他例子。它们与我的代码相同。你知道问题出在哪里吗?
您的 Client
class 扩展了 Model
,它不支持在数据库中保存数据,因此未定义 save()
方法。如果你想使用数据库记录,你的模型应该扩展 ActiveRecord
:
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',
];
}
}
您好,我是 Yii 框架的新手,我正在尝试将数据保存到数据库。不会用不知道是哪里的问题
控制器:
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,]);
}
}
查看:
<?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',
];
}
}
我已经用迁移创建了数据库。但我不明白为什么会发生这个错误。我应该在模型中包含一些保存方法还是如何解决这个问题。我也看了其他例子。它们与我的代码相同。你知道问题出在哪里吗?
您的 Client
class 扩展了 Model
,它不支持在数据库中保存数据,因此未定义 save()
方法。如果你想使用数据库记录,你的模型应该扩展 ActiveRecord
:
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',
];
}
}