L
L
lilwings2020-01-08 03:23:36
Yii
lilwings, 2020-01-08 03:23:36

Yii2 editing news with photo?

I did it like this:
Model:

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,
            ],
        ];
    }

Controller:
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'));
}

That is, I separately check whether the photo is uploaded separately and, if necessary, fill in the required attribute for saving, is this the norm?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question