N
N
Nikolai2019-06-08 15:47:01
Yii
Nikolai, 2019-06-08 15:47:01

How to upload and display the second image?

Hello, with one picture I figured out how to upload it and display it, but now you need to upload the second one in the product card as well. The second picture is loaded, but I have trouble with its output. I am using this .
So far, everything is in the admin part.
In the controller of product cards, I registered when we update (so far):

public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
      $model->img = UploadedFile::getInstance($model, 'img'); // Фото к маленькой карточки товара
      $model->big = UploadedFile::getInstance($model, 'big'); // Фото к большой карточки товара
      if ($model->img) {
        $model->upload();
      } elseif ($model->big) {
        $model->uploadBig();
      }
      
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

In the product card model, I registered the behavior, set the fields and functions for saving images:
public function behaviors()
    {
        return [
            'image' => [
                'class' => 'rico\yii2images\behaviors\ImageBehave',
            ]
        ];
    }

    public function rules()
    {
        return [
            // другие строки...
      [['img'], 'file', 'extensions' => 'png, jpg'],
    	    [['big'], 'file', 'extensions' => 'png, jpg'],
        ];
    }

  public function upload() {
    if ($this->validate()) {
      $path = 'img/upload/' . $this->img->baseName . '.' . $this->img->extension; // Путь сохранения файла (папка - имя файла - расширение его).
      $this->img->saveAs($path);
      $this->attachImage($path, true);
      @unlink($path);
      return true;
    } else {
      return false;
    }
  }
  public function uploadBig() {
    if ($this->validate()) {
      $path = 'img/upload/' . $this->big->baseName . '.' . $this->big->extension; // Путь сохранения файла (папка - имя файла - расширение его).
      $this->big->saveAs($path);
      $this->attachImage($path);
      @unlink($path);
      return true;
    } else {
      return false;
    }
  }

In the product view (view) and in the fields where the picture should be:
<?php $img = $model->getImage(); $big = $model->getImage();?>

//            'image',
      [
        'attribute' => 'img',
        'value' => '<img src="../../web/img/upload/' . $img->filePath . '" class="admin__img-view">',
        'format' => 'html',
      ],
//            'image_big',
      [
        'attribute' => 'big',
        'value' => '<img src="../../web/img/upload/' . $big->filePath . '" class="admin__img-view">',
        'format' => 'html',
      ],

But something does not work out to get the second picture (address to it). What am I doing wrong?

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