Answer the question
In order to leave comments, you need to log in
How to pass the correct parameters to save data to the database?
Folks, good time. My stupid head does not understand anything, tell me what I'm doing wrong?
Model
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "films".
*
* @property int $id
* @property string $title
* @property string $desk
* @property string $text
* @property string $id_alias
* @property string $id_category
* @property string $id_theater
* @property string $data_out
* @property string $budget
* @property string $author
* @property string $imageFile
*
* @property Categoryes $category
* @property Theaters $theater
* @property Categoryes $alias
*/
class Films extends \yii\db\ActiveRecord
{
public $imageFile;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'films';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['title', 'desk', 'text', 'id_alias', 'id_category', 'id_theater', 'data_out', 'budget', 'author','imageFile'], 'required'],
[['text'], 'string'],
[['data_out'], 'safe'],
[['imageFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
[['title', 'desk', 'id_alias', 'id_category', 'id_theater', 'budget', 'author'], 'string', 'max' => 255],
[['id_category'], 'exist', 'skipOnError' => true, 'targetClass' => Categoryes::className(), 'targetAttribute' => ['id_category' => 'category']],
[['id_theater'], 'exist', 'skipOnError' => true, 'targetClass' => Theaters::className(), 'targetAttribute' => ['id_theater' => 'theater']],
[['id_alias'], 'exist', 'skipOnError' => true, 'targetClass' => Categoryes::className(), 'targetAttribute' => ['id_alias' => 'alias']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'desk' => 'Desk',
'text' => 'Text',
'id_alias' => 'Id Alias',
'id_category' => 'Id Category',
'id_theater' => 'Id Theater',
'data_out' => 'Data Out',
'budget' => 'Budget',
'author' => 'Author',
'imageFile' => 'Image File',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Categoryes::className(), ['category' => 'id_category']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTheater()
{
return $this->hasOne(Theaters::className(), ['theater' => 'id_theater']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAlias()
{
return $this->hasOne(Categoryes::className(), ['alias' => 'id_alias']);
}
public function upload()
{
if($this->validate()){
$this->imageFile->saveAs("img/{$this->imageFile->baseName}.{$this->imageFile->extension}");
return true;
} else {
return false;
}
}
}
public function actionCreate()
{
$model = new Films();
if ($model->load(Yii::$app->request->post())) {
$model->imageFile = UploadedFile::getInstance($model,'imageFile');
if($model->imageFile && $model->upload()) {
$model->imageFile = $this->imageFile->baseName . '.' . $this->imageFile->extension;
}
if($model->save()) {
return $this->redirect(['creat', 'id' => $model->id]);
}
}
return $this->render('create', [
'model' => $model,
]);
}
<?php
$params = [
'promt' => 'не указано'
]
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'desk')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'text')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'id_category')->dropDownList(ArrayHelper::map(Categoryes::find()->all(),'category','category'),$params) ?>
<?= $form->field($model, 'id_alias')->dropDownList(ArrayHelper::map(Categoryes::find()->all(),'alias','alias'),$params) ?>
<?= $form->field($model, 'data_out')->textInput() ?>
<?= $form->field($model, 'budget')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'id_theater')->dropDownList(ArrayHelper::map(Theaters::find()->all(),'theater','theater'),$params) ?>
<?= $form->field($model, 'imageFile')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Answer the question
In order to leave comments, you need to log in
Good evening.
The error says "does not have a default value".
Remove imageFile from the required section in the validation rules or in the same rules, assign the default value through the default parameter.
Note: Most validators do not process empty inputs if their [[yii\base\Validator::skipOnEmpty]] property defaults to true. They will simply be skipped during validation if their associated attributes are empty. Among the main validators, only captcha, default, filter, required, and trim will handle empty inputs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question