Search code examples
phpfilesymfonyvichuploaderbundle

Vichuploader File is not saved with post request


my project is actually to create a website that permit me to post gif, jpg etc..

I'm using VichuploaderBundler to upload a file and save it to a specific repository in my app

#vich uploader Configuration
vich_uploader:
     db_driver: orm
     mappings:
        product_image:
            uri_prefix:         /images/posts
            upload_destination: '%kernel.root_dir%/../web/images/posts'
            namer:              vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true

I'm creating an API to create and render ressources.

I'm trying to create a Post with a post request with postman. In this Post i want to save an image throw vichuploader.(this had worked with a classical form).

Here is my problem :

My post is created thx to my postman request. But the file that i upload is not saved in the

                 /**
                 * @Rest\View(statusCode=Response::HTTP_CREATED)
                 * @Rest\Post("/posts")
                 */
                public function postPostsAction(Request $request)
                {
                    $post = new Post();
                    $post->setTitle($request->get('title'))
                        ->setDescription($request->get('description'))
                        ->setImageName($request->get('image_name'))
                        ->setImageFile($request->get('imageFile'))

                        // It is required that at least one field changes if you are using doctrine
                        // otherwise the event listeners won't be called and the file is lost
                        ->setUpdatedAt(new \DateTime());


                    $em = $this->get('doctrine.orm.entity_manager');
                    $em->persist($post);
                    $em->flush();

                    return $post;
                }

Here is my postman

Thx for helping and feedback about my post.


Solution

  • I find how to fixed this, it was very simply in fact !

    public function postPostsAction(Request $request)
    {
        $post = new Post();
        $post->setTitle($request->get('title'))
            ->setDescription($request->get('description'))
            ->setImageName($request->get('image_name'))
            ->setImageFile($request->files->get('imageFile'))
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            ->setUpdatedAt(new \DateTime());
    
    
        // On fait appel à doctrine pour seter un id lors de la création en base
        $em = $this->get('doctrine.orm.entity_manager');
        $em->persist($post);
        $em->flush();
    
        return $post;
    }
    

    Thanks to the ->files statement my pictures are saved in the right directory