简体   繁体   English

Sonata管理文件上传:除了管理面板之外,还上传文件

[英]Sonata admin file uploads : Upload files apart from the admin panel

I have a Symfony 2.8 based project, I have Sonata admin bundle and Sonata user bundle well installed and all is working good. 我有一个基于Symfony 2.8的项目,已经安装了Sonata管理员捆绑包和Sonata用户捆绑包,并且一切正常。

I have an "Image" entity that it meant to contain my uploaded file. 我有一个“图片”实体,它打算包含我上传的文件。 I followed the official Sonata tutorial to how to upload a file ( https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html ) and all is working perfectly when I want to upload a file from the admin panel. 我遵循了Sonata官方教程,了解如何上传文件( https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html ),当我想从以下位置上传文件时,一切运行正常管理面板。

Now, I need a to offer the possibility to a simple connected user (not admin) to upload a file as well from a form. 现在,我需要向简单的连接用户(而非管理员)提供一种从表单上载文件的可能性。

Here's the example I have: 这是我的示例:

I have this "Offer" class that has an "Image" attribute: 我有一个具有“图像”属性的“提供”类:

class Offer {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var string
     *
     * @ORM\Column(name="body", type="text")
     */
    private $body;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    /**
     * @ORM\OneToOne(targetEntity="AIEM\PlatformBundle\Entity\Image", cascade={"all"})
     */
    private $image;

   //Getters and Setters
}

Before adding the "Image" entity, I persisted an offer using the classic way : Getting my data from the request (example : $offer->setTitre($request->request->get('title')); ). 在添加“ Image”实体之前,我使用经典方式坚持了要约:从请求中获取数据(例如: $offer->setTitre($request->request->get('title')); )。 But now since I have a OneToMany with a file, I don't know how to proceed. 但是现在由于我有一个带文件的OneToMany,所以我不知道如何继续。

I'll be grateful if you can share some ideas. 如果您能分享一些想法,我将不胜感激。

EDIT Here's my OfferAdmin, and it's working perfectly in sonata admin 编辑这是我的OfferAdmin,它在奏鸣曲管理中完美运行

class OfferAdmin extends Admin {

    protected function configureFormFields(FormMapper $formMapper) {
        $formMapper->add('title', 'text')
                ->add('image', 'sonata_type_admin')
                ->add('body', 'textarea', array("attr" => array("class" => "ckeditor")));
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) {
        $datagridMapper->add('titre')
                ->add('date');
    }

    protected function configureListFields(ListMapper $listMapper) {
        $listMapper->addIdentifier('titre');
    }

    public function prePersist($page) {
        $this->manageEmbeddedImageAdmins($page);
    }

    public function preUpdate($page) {
        $this->manageEmbeddedImageAdmins($page);
    }

    private function manageEmbeddedImageAdmins($page) {

        /** @var Image $image */
        $image = $page->getImage();

        if ($image) {
            if ($image->getFile()) {
                // update the Image to trigger file management
                $image->refreshUpdated();
            } elseif (!$image->getFile() && !$image->getFilename()) {
                // prevent Sf/Sonata trying to create and persist an empty Image
                $page->$setImage(null);
            }
        }
    }

}

And the form I want the user to add the file from looks like this 我希望用户从中添加文件的表单如下所示

<form method="post" action="{{ path('aiem_platform_add_offer')}}">
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" id="title" name="title" placeholder="Title">
    </div>


    <div class="form-group">
        <input type="file" class="form-control" id="myFile" name="myFile">
    </div>

    <div class="form-group">
        <label for="contenu">Contenu</label>
        <textarea class="form-control ckeditor" id="body" name="body"></textarea>
    </div>

    <button type="submit" class="btn btn-default">Add</button>
</form>

Thank you 谢谢

Use something like the following for the form handling method : 对表单处理方法使用类似以下的内容:

public function handleOfferForm(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $offer = new Offer();
    // Handle your basic fields using $request->request->get('field')

    $file = $request->files->get('myFile');

    $image = new Image();
    $image->setFile($file);
    // Set your other fields ...
    $image->upload(); // Image should have this method (from the sonata doc)

    $offer->addImage($image);

    $em->persist($offer);
    $em->flush();

    // Return a redirection or which response you want 
}

Also, create the corresponding POST route in your routing (named like in your form action), and it should works. 另外,在您的路由中创建相应的POST路由(命名为表单动作中的名称),它应该可以工作。

EDIT 编辑

Make your form open tag like follows : 使您的窗体打开标签,如下所示:

<form enctype="multipart/form-data">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM