Всем привет, я новичок в yii и я сгенерировал код для связи OneToMany и у меня во views есть форма с возможностью обновить или сохранить данные в Бд, но они никак не сохраняются помогите разобраться пожалуйста
<?php
namespace frontend\models;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "authors".
*
* @property int $id
* @property string $name
* @property string $patronymic
* @property string $surname
* @property string $created_at
* @property string $updated_at
*
* @property Books[] $books
*/
class Author extends ActiveRecord
{
public static function tableName()
{
return 'authors';
}
public function rules()
{
return [
[['name', 'created_at', 'updated_at'], 'required'],
[['created_at', 'updated_at'], 'safe'],
[['name', 'patronymic', 'surname'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'patronymic' => 'Patronymic',
'surname' => 'Surname',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
public function getBooks()
{
return $this->hasMany(Books::className(), ['id_author' => 'id']);
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
}
/**
* AuthorController implements the CRUD actions for author model.
*/
class AuthorController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all author models.
* @return mixed
*/
public function actionIndex()
{
$books = Author::find()->one()->getBooks()->asArray()->all();
$searchModel = new SearchAuthor();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'books' => $books
]);
}
/**
* Displays a single author model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new author model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Author();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing author model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing author model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the author model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Author the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Author::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model frontend\models\Author */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="author-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'patronymic')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'surname')->textInput(['maxlength' => true]) ?>
<?/*= $form->field($model, 'created_at')->textInput() */?><!--
--><?/*= $form->field($model, 'updated_at')->textInput() */?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
[['name', 'created_at', 'updated_at'], 'required'],