zxbodya / yii2-gallery-manager

93 stars 61 forks source link

Extension does not condiser type option #41

Closed nihaha closed 7 years ago

nihaha commented 7 years ago

I have two models - products and services. For both I've configured galleryBehavior option accordingly:

public function behaviors()
    {
        Image::$driver = [Image::DRIVER_IMAGICK];
        return [
            'galleryBehavior' => [
                'class' => GalleryBehavior::className(),
                'type' => 'service',
                'extension' => 'jpg',
                'directory' => Yii::getAlias('@webroot') . '/upload/images/service',
                'url' => Yii::getAlias('@web') . '/upload/images/service',
                'versions' => [
                    ...
                ]
            ],
        ];
    }

and

public function behaviors()
    {
        Image::$driver = [Image::DRIVER_IMAGICK];
        return [
            'galleryBehavior' => [
                'class' => GalleryBehavior::className(),
                'type' => 'product',
                'extension' => 'jpg',
                'directory' => Yii::getAlias('@webroot') . '/upload/images/product',
                'url' => Yii::getAlias('@web') . '/upload/images/product',
                'versions' => [
                    ...
                ]
            ],
        ];
    }

Images are saving correctly - each gallery to its directory but when I try to display them in my views I get strange behavior: images will be loading without considering a type.

In my view I render the gallery as follows:

<?php $images = $service->images; ?>

<?php foreach($images as $image) : ?>
    <?= Html::img("/upload/images/{$image->type}/{$image->ownerId}/{$image->id}/full_image.jpg") ?>
<?php endforeach; ?>

What is does is it loads all of the images with suited ownerId without noticing a type field.

zxbodya commented 7 years ago

Do not quite get a question…

Likely issue is about $service->images in you model… How is it defined? (might be you need to add type to you db query there)

Generally I would not recommend doing this way… Instead - you methods in behavior, like in sample from readme:

foreach($model->getBehavior('galleryBehavior')->getImages() as $image) {
    echo Html::img($image->getUrl('medium'));
}
nihaha commented 7 years ago

Yes, I've considered to tweak my query in the model but for some reason I thought that attached behavior will do the job. Thanks, will fix my output.