Сделал вот так:
Модель:
namespace backend\models;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
class Info extends ActiveRecord
{
public $img_previewT;
public $img_mainT;
public $img_preview_name;
public $img_main_name;
public static function tableName()
{
return '{{info}}';
}
public function rules()
{
return [
[['title', 'preview', 'description_seo'], 'filter', 'filter' => 'trim'],
[['title', 'preview', 'description_seo'], 'filter', 'filter' => 'strip_tags'],
[['title', 'preview', 'description'], 'required'],
['slug', 'safe'],
[['img_main', 'img_preview'], 'safe'],
[['img_previewT', 'img_mainT'], 'safe'],
];
}
public function attributeLabels()
{
return [
'title' => 'Заголовок',
'preview' => 'Превью',
'img_previewT' => 'Превью фотографии',
'img_mainT' => 'Основная фотография',
'description' => 'Описание',
'description_seo' => 'Description (SEO)',
];
}
public function uploadPreview()
{
if (is_object($this->img_previewT) && $this->img_previewT->error === 0) {
$this->img_preview_name = md5(uniqid(rand(),true));
$this->img_previewT->saveAs(Yii::getAlias('@frontend') . '/web/images/info/' . $this->img_preview_name . '.' . $this->img_previewT->extension);
$this->img_preview_name = '/images/info/' . $this->img_preview_name . '.' . $this->img_previewT->extension;
return true;
} else {
return false;
}
}
public function uploadMain()
{
if (is_object($this->img_mainT) && $this->img_mainT->error === 0) {
$this->img_main_name = md5(uniqid(rand(),true));
$this->img_mainT->saveAs(Yii::getAlias('@frontend') . '/web/images/big_info/' . $this->img_main_name . '.' . $this->img_mainT->extension);
$this->img_main_name = '/images/big_info/' . $this->img_main_name . '.' . $this->img_previewT->extension;
return true;
} else {
return false;
}
}
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'title',
'ensureUnique' => true,
],
];
}
Контроллер:
public function actionAdd()
{
$model = new Info();
if ($model->load(Yii::$app->request->post())) {
$model->img_mainT = UploadedFile::getInstance($model, 'img_mainT');
$model->img_previewT = UploadedFile::getInstance($model, 'img_previewT');
if ($model->uploadMain()) {
$model->img_main = $model->img_main_name;
}
if ($model->uploadPreview()) {
$model->img_preview = $model->img_preview_name;
}
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Информация добавлена!');
return $this->refresh();
}
Yii::$app->session->setFlash('error', 'Не удалось добавить информацию!');
}
return $this->render('add', compact('model'));
}
public function actionEdit($id) {
$model = Info::find()->where(['id' => $id])->one();
if ($model->load(Yii::$app->request->post())) {
$model->img_mainT = UploadedFile::getInstance($model, 'img_mainT');
$model->img_previewT = UploadedFile::getInstance($model, 'img_previewT');
if ($model->uploadMain()) {
$model->img_main = $model->img_main_name;
}
if ($model->uploadPreview()) {
$model->img_preview = $model->img_preview_name;
}
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Информация обновлена!');
return $this->refresh();
}
Yii::$app->session->setFlash('error', 'Не удалось обновить информацию!');
}
return $this->render('edit', compact('model'));
}
Т.е отдельно проверяю отдельно загружена ли фотография и если нужно то заполняю нужный аттрибут для сохранения, норма ли это?