VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

dynamic mapping

Open peterkracik opened this issue 4 years ago • 12 comments

Q A
Version 1.10.1

Support Question

Is there a possibility to have dynamic mapping? to explain, I've created a class Image, which is used in other classes like: Product (for product image) and Company (company logo). and I would like to set mapping based on the class where it's mapped.

Image.php

/**
 * @MongoDB\EmbeddedDocument
 * @MongoDB\HasLifecycleCallbacks
 * @Vich\Uploadable
 */
class Image
{
    /**
     * @MongoDB\Id()
     */
    protected $id;

    /**
     * @Vich\UploadableField(**mapping="product",** fileNameProperty="imageName", size="imageSize")
     * @var File
     */
    private $imageFile;

     /**
     * @MongoDB\Field(type="string")
     */
    private $imageName;

    /**
     * @MongoDB\Field(type="integer")
     */
    private $imageSize;

    /**
     * @MongoDB\Field(type="date")
     */
    private $updatedAt;

    private $finalName;

and then classes like Company.php

/**
 * @MongoDB\Document
 */
class Company
{
    /**
     * @MongoDB\EmbedOne(targetDocument="Image")
     * @var Image
     */
    protected $logo;
 ...

and Product

/**
 * @MongoDB\Document
 */
class Product
{
    /**
     * @MongoDB\EmbedOne(targetDocument="Image")
     * @var Image
     */
    protected $image;
 ...

and I would like different mapping for these two classes, or at least to have a possibility to use different folder. is there a way to achieve this?

thank you for the help

peterkracik avatar Sep 01 '19 08:09 peterkracik

Just define your mappings. I can't see any reason to be "dynamic" here

garak avatar Sep 01 '19 08:09 garak

thank you @garak for your fast reply.

the thing is that I like to have a different mapping based on the class where it is used. mapping "product" for image in product class and mapping "logo" for image in company class. because I want to avoid create child classes like ProductImage, CompanyLogoImage etc just to have a different folder (mapping).

peterkracik avatar Sep 01 '19 08:09 peterkracik

I see. Try to define your mapping on $image property of Product and Company and remove mapping from $imageFile in Image. Something like:

@Vich\UploadableField(mapping="product", fileNameProperty="image.imageName")

garak avatar Sep 01 '19 08:09 garak

no, unfortunately it doesnt work. I have error 500 - Mapping not found for field "imageFile"

or maybe some other way how to set mapping? for example in the form? (i use sonata admin), or I tried to create a custom annotation which property I could get inside the image class, I didnt success but maybe it's a way to do it? i've spent all day yesterday to find a way how to do it properly, but couldn't figure it out and I suppose I am not the first one with this problem :)

peterkracik avatar Sep 01 '19 11:09 peterkracik

Your mapping is not on imageFile anymore, it's on image

garak avatar Sep 01 '19 13:09 garak

yes, its what I did

Image.php

/**
 * @MongoDB\EmbeddedDocument
 * @MongoDB\HasLifecycleCallbacks
 * @Vich\Uploadable
 */
class Image
{
    /**
     * @MongoDB\Id()
     */
    protected $id;

    /**
     * @var File
     */
    private $imageFile;

     /**
     * @MongoDB\Field(type="string")
     */
    private $imageName;

    /**
     * @MongoDB\Field(type="integer")
     */
    private $imageSize;

    /**
     * @MongoDB\Field(type="date")
     */
    private $updatedAt;

    private $finalName;

Product.php

/**
 * @MongoDB\Document
 */
class Product
{
    /**
     * @MongoDB\EmbedOne(targetDocument="Image")
     * @Vich\UploadableField(mapping="product", fileNameProperty="imageName", size="imageSize")
     * @var Image
     */
    protected $image;
 ...

and this returns me that error

peterkracik avatar Sep 01 '19 14:09 peterkracik

fileNameProperty="image.imageName" (as stated before)

garak avatar Sep 01 '19 14:09 garak

I've tried it but unfortunately it doesnt work, because Image in my Product class is not really uploadable field, only object which one contains uplodable file. and this one needs the mapping in the annotation, not the object in the Product class. if its not there I've got the mentioned error. and the imageFile (within Image class) doesnt 'inherit' the mapping from the image property in the Product class.

Class Product

  • id :int
  • name :string
  • image :Image ...

Image.php

  • id :int
  • imageName :string
  • imageFile :file (uploadableFile (mapping="") ) ...

or maybe I am just trying what's not possible with vichUploader?

peterkracik avatar Sep 01 '19 18:09 peterkracik

It is possible, I'm using myself this bundle with an embedded value object.

Unfortunately, in my project there's no Mongo (I'm using Doctrine ORM) and there are no annotations (I'm using yaml mapping instead)

garak avatar Sep 02 '19 12:09 garak

Hi @garak , I using Doctrine and have the same issue. Could you please describe how I can solve it?

I want to use 1 Entity (MediaObject ) and relate it with several other Entities (User, Product, etc). But files need to be stored at the different directories ( regarding mapping )

Akekc avatar Jul 20 '21 12:07 Akekc

Could you please describe how I can solve it?

I didn't solve it. Dynamic mapping is not supported at the moment.

garak avatar Jul 27 '21 07:07 garak

Damn, well, thanks for the answer anyway)

27 июля 2021 г., в 10:18, Massimiliano Arione @.***> написал(а):

Could you please describe how I can solve it?

I didn't solve it. Dynamic mapping is not supported at the moment.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dustin10/VichUploaderBundle/issues/1027#issuecomment-887273786, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQHJD7R7RYD2HBXCLUSEFLTZZMT5ANCNFSM4ISWB2MQ.

Akekc avatar Jul 27 '21 07:07 Akekc