Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload Field #396

Closed
boonkerz opened this issue Mar 28, 2013 · 14 comments
Closed

Upload Field #396

boonkerz opened this issue Mar 28, 2013 · 14 comments

Comments

@boonkerz
Copy link

Hey,

after upload an file the follow error occurs:

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.

I think its related to symfony 2.2 or?

Regards

@ioleo
Copy link
Member

ioleo commented Mar 28, 2013

@boonkerz check out updated Upload docs section 4 Edit your entity class

Upload type extends Collection type, which needs data_class specified

@boonkerz
Copy link
Author

@ioleo
Copy link
Member

ioleo commented Mar 28, 2013

@boonkerz I thought you use Upload type :) I updated the docs see here - yes, you must add data_class: Symfony\Component\HttpFoundation\File\File in single upload type

@ioleo ioleo closed this as completed Mar 28, 2013
@battika
Copy link
Contributor

battika commented Mar 30, 2013

@loostro I'm trying to get single_upload work and create a cookbook entry on it but I can't figure out the last step that involves editing of the generator.yml file. When you got some time, can you please have a look into my draft document and advise where to go from here? Thanks, It's very much appreciated.

@boonkerz
Copy link
Author

# generator.yml
    image:
        label: Bild
        formType:         single_upload
        dbType:           string
        addFormOptions:
            data_class:     Symfony\Component\HttpFoundation\File\File
# Entity:

    /**
     *
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="image_path")
     */
    private $image;

    /**
     * @var string $image
     *
     * @ORM\Column(name="image_path", type="string", length=255, nullable=true)
     */
    private $image_path;

@ioleo
Copy link
Member

ioleo commented Apr 2, 2013

@battika see @boonkerz 's post above, for Vich uploader to work you must add private $image_path; property where your uploaded file's path will be stored

About the generator.yml config, here is a working example from my project:

    image:
      label:            label.image
      formType:         single_upload
      dbType:           string
      addFormOptions:
        nameable:       imageLabel
        data_class:     Symfony\Component\HttpFoundation\File\File

and the entity behind it:

    /**
     * @Assert\File(
     *     maxSize="12M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Assert\Image(
     *     minWidth = 280,
     *     minHeight = 100
     * )
     * @Vich\UploadableField(mapping="category_image", fileNameProperty="imagePath")
     */
    protected $image;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $imageLabel;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $imagePath;

and my vich_uploader config:

vich_uploader:
    db_driver:  orm
    gaufrette:  false
    storage:    vich_uploader.storage.file_system
    mappings:
        category_image:
            uri_prefix:           /media/category/image
            upload_destination:   %kernel.root_dir%/../web/media/category/image
            namer:                vich_uploader.namer_uniqid
            inject_on_load:       true
            delete_on_remove:     true
            delete_on_update:     true

As you can see I used vich_uploader.namer_uniqid namer, which will store files useing their unique ID. So my jamie_photo.jpg attached to Category ID 15 will be stored as 15, path will be .jpg, and original file name will be lost.

Thats why I introduced the (optional) nameable option. You can specify a property in which original filename info will be saved. Also, this filename can be edited, so if you've uploaded my_ugly_file.jpg and want to rename it - no problem, you dont have to reupload it.

In future i hope to push a PR to Vich bundle which will allow to use a property to serve the file back to the user with our stored filename. Right now downloading that file will give user a 15.jpg. I hope to make Vich "aware" of our stored filename and give the user a jamie_photo.jpg

@battika
Copy link
Contributor

battika commented Apr 3, 2013

@loostro @boonkerz Thanks very much for your quick help. I set up everything identical to @loostro 's configuration but when I set up my generator.yml like this:

    new:
        params:
            title: New object for ProductBundle
            display: 
                - name
                - price
                - image
            actions:
                save: ~
                list: ~

I'm getting an error: Property "image" is not public in class "Acme\ProductBundle\Entity\Product". Maybe you should create the method "getImage()" or "isImage()" or "hasImage()"?

When I replace image with imageLabel I can see the field but it comes up as a simple text field as opposed to the uploader widget.

Can you please help what I'm doing wrong? Thank you.

@ioleo
Copy link
Member

ioleo commented Apr 4, 2013

@battika you should create getters and setters for your properties:

for each property (e.g. image) you must have 2 methods in entity class:

  • getXxx where Xxx is camelized property name, eg. getImage
  • setXxx where Xxx is camelized property name, eg. setImage

for collection properties you must have 3 methods, eg. images collection:

  • addXxx, eg. addImage -> adds object to collection
  • removeXxx, eg. removeImage -> removes object from collection
  • getXxxs, eg. getImages -> returns collection

You can write them on your own or simply use php app/console doctrine:generate:entities Acme/MyBundle/Entity/MyEntity command see symfony docs

@boonkerz
Copy link
Author

boonkerz commented Apr 4, 2013

And we have the same Issues:
dustin10/VichUploaderBundle#123
dustin10/VichUploaderBundle#106

@ioleo
Copy link
Member

ioleo commented Apr 4, 2013

i think dustin10/VichUploaderBundle#106 will solve your problem (useing preUpdate event)

@boonkerz
Copy link
Author

boonkerz commented Apr 4, 2013

Yea but is not merged :(

@battika
Copy link
Contributor

battika commented Apr 7, 2013

@loostro thanks I'm aware of getters and setters for some reason they never get created for $image, even when I specified @var File $image in the annotations:

    /**
     * @Assert\File(
     *     maxSize="12M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )

     * @Vich\UploadableField(mapping="product_image", fileNameProperty="imagePath")
     * 
     * @var File $image
     * 
     */
    protected $image;

So, I had to create the getter and setter manually. Thanks for the tip, it works like a charm now.

@ioleo
Copy link
Member

ioleo commented Apr 7, 2013

good! i hope you like this widget =) its one of my favourite =)

@battika
Copy link
Contributor

battika commented Apr 8, 2013

@loostro thank you, I like it very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants