简体   繁体   English

使用Sonata和Symfony 3获取实体中的路径

[英]Get path in entity with Sonata and Symfony 3

I need to get the path of the web/uploads folder from the entity, this is my code: 我需要从实体获取web / uploads文件夹的路径,这是我的代码:

<?php

class Product{
  protected $id;
  ...
  protected $imageName;

  protected $file;
  ...
  public function getAbsolutePath(){
      return null === $this->imageName ? null : $this->getUploadRootDir().'/'.$this->imageName;
  }

  public function getWebPath(){
    return null === $this->imageName ? null : $this->getUploadDir().'/'.$this->imageName;
  }

  protected function getUploadRootDir($basepath){
    // the absolute directory path where uploaded documents should be saved
    return $basepath.$this->getUploadDir();
  }

  protected function getUploadDir(){
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
    return 'uploads/products';
  }

  public function upload($basepath){
    // the file property can be empty if the field is not required
    if (null === $this->file) {
        return;
    }

    if (null === $basepath) {
        return;
    }

    // we use the original file name here but you should
    // sanitize it at least to avoid any security issues

    // move takes the target directory and then the target filename to move to
    $this->file->move($this->getUploadRootDir($basepath), $this->file->getClientOriginalName());

    // set the path property to the filename where you'ved saved the file
    $this->setImageName($this->file->getClientOriginalName());

    // clean up the file property as you won't need it anymore
    $this->file = null;
  }
}

And this is the Admin class 这是Admin类

<?php

class Product extends Admin {
...
  protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->with('General')
...
            ->add('file', 'file', array('required' => false))
...
            ->end()
    ;
  }
...
  public function prePersist($product) {
    $this->saveFile($product);
  }

  public function preUpdate($product) {
    $this->saveFile($product);
  }

  public function saveFile($product) {
    $basepath = $this->getRequest()->getBasePath();
    $product->upload($basepath);
  }
}

The name of the file is updated well, but the image don't copy at the path web/uploads. 文件名更新得很好,但是图像不会在web / upload路径下复制。 source: http://blog.code4hire.com/2011/08/symfony2-sonata-admin-bundle-and-file-uploads/ 来源: http : //blog.code4hire.com/2011/08/symfony2-sonata-admin-bundle-and-file-uploads/

I have solved this way 我已经解决了

<?php

class Product{

  const SERVER_PATH_TO_IMAGE_FOLDER = '/server/path/to/images';

  protected $id;
...
  protected $imageName;

  protected $file;
...

  public function upload($basepath){
    if (null === $this->file) {
        return;
    }

    $this->file->move(self::SERVER_PATH_TO_IMAGE_FOLDER, $this->file->getClientOriginalName());

    // set the path property to the filename where you'ved saved the file
    $this->setImageName($this->file->getClientOriginalName());

    // clean up the file property as you won't need it anymore
    $this->file = null;
  }
}

And the admin class: 和管理类:

<?php

class Product extends Admin {
...
  protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->with('General')
...
            ->add('file', 'file', array('required' => false))
...
            ->end()
    ;
  }
...
  public function prePersist($product) {
    $product->upload();
  }

}

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

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