Vichuploader文件未随帖子请求一起保存

时间:2017-02-16 18:22:36

标签: php file symfony vichuploaderbundle

我的项目实际上是创建一个允许我发布gif,jpg等的网站。

我使用VichuploaderBundler上传文件并将其保存到我应用中的特定存储库

#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

我正在创建一个API来创建和呈现资源。

我正在尝试使用邮递员创建一个包含帖子请求的帖子。 在这篇文章中我想保存一个图像throw vichuploader。(这曾经使用过古典形式)。

这是我的问题:

我的帖子是根据我的邮递员请求创建的。但我上传的文件未保存在

                 /**
                 * @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

帮助和反馈我的帖子。

1 个答案:

答案 0 :(得分:1)

我发现如何解决这个问题,实际上非常简单!

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;
}

感谢 - > files语句,我的图片保存在正确的目录中