How to add an image to a model?

How to add an image to a model?

SkeekS CMS has a file storage component. Each downloaded file in this repository is stored in a database and has its own data model.

Imagine that we have a product Product. And we want to upload an image to the file storage and link it to the product.

The problem is solved as follows.

1) The Product model must contain the image_id (int) property

2) Product model must have a behavior

<?php class Product extends \yii\db\ActiveRecord { /** * @return array */ public function behaviors() { return array_merge(parent::behaviors(), [ \skeeks\cms\models\behaviors\HasStorageFile::className() => [ 'class' => \skeeks\cms\models\behaviors\HasStorageFile::className(), 'fields' => ['image_id'] ] ]); } } 

And the corresponding widget is actually used in the form:

<?= $form->field($product, 'image_id')->widget( \skeeks\cms\widgets\formInputs\StorageImage::className() ); ?> 

That's all, image_id - can be called anything, and there can be as many fields as you like. image1_id image2_id ...

All Comments (0)
No Comments