Symfony2文件上传:如何在原则中保存文件路径



我在从文件上传中保存原则中的路径时遇到问题。我使用了这里的例子:如何使用原则处理文件上传

上传有效,但我没有将文件路径保存到数据库中,我认为回调不起作用。至少我的上传没有文件扩展名。

这是我的控制器:

   public function uploadAction()
{
$document = new Document();
//$form   = $this->createForm(new DocumentType(), $entity);

$form = $this->createFormBuilder($document)
    ->add('name')
    ->add('file')
    ->getForm()
;
if ($this->getRequest()->getMethod() === 'POST') {
    $form->bindRequest($this->getRequest());
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($document);
        $em->flush();
        $this->redirect($this->generateUrl('document'));
    }
}
return  $this->render("ISClearanceBundle:Document:upload.html.twig",array('form' =>   $form->createView()));
}

这是实体:

 <?php
   namespace ISClearanceBundleEntity;
   use DoctrineORMMapping as ORM;
   use SymfonyComponentHttpFoundationFileUploadedFile;
   use SymfonyComponentValidatorConstraints as Assert;
  /**
   * ISClearanceBundleEntityDocument
   * @ORMEntity
   * @ORMHasLifecycleCallbacks
   */
  class Document
 {
 /**
  * @var string $name
  */
private $name;
/**
 *  @ORMColumn(type="string", length=255, nullable=true)
 */
public $path;
/**
 * @var integer $projectId
 */
private $projectId;
/**
 * @var integer $id
 */
private $id;
 /**
 * @AssertFile(maxSize="6000000")
 */
public $file;
/**
 * Set name
 *
 * @param string $name
 */
public function setName($name)
{
    $this->name = $name;
}
/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}
/**
 * Set path
 *
 * @param integer $path
 */
public function setPath($path)
{
    $this->path = $path;
}
/**
 * Get path
 *
 * @return integer
 */
public function getPath()
{
    return $this->path;
}
/**
 * Set projectId
 *
 * @param integer $projectId
 */
public function setProjectId($projectId)
{
    $this->projectId = $projectId;
}
/**
 * Get projectId
 *
 * @return integer
 */
public function getProjectId()
{
    return $this->projectId;
}
/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}
 public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return __DIR__.'/../../../../web/'.$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/documents';
}
/**
 * @ORMPrePersist()
 * @ORMPreUpdate()
 */
public function preUpload()
{
    if (null !== $this->file) {
        // do whatever you want to generate a unique name
        $this->path = uniqid().'.'.$this->file->guessExtension();
    }
        $this->path = uniqid().'.'.$this->file->guessExtension();
}
/**
 * @ORMPostPersist()
 * @ORMPostUpdate()
 */
public function upload()
{
    if (null === $this->file) {
        return;
    }
    // if there is an error when moving the file, an exception will
    // be automatically thrown by move(). This will properly prevent
    // the entity from being persisted to the database on error
    $this->file->move($this->getUploadRootDir(), $this->path);
    $this->setPath($this->path);
    unset($this->file);
}
/**
 * @ORMPostRemove()
 */
public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
 }
}

我真的不知道如何保存数据库的路径以及如何调试。

感谢您的任何帮助!

看看 upload() 方法。它具有postPersist(),这意味着实体更改不会保存到数据库中,因此更改的属性不会存储在当前保存中。

您必须使用 prePersist 注释更新方法中的路径,并使用 postPersist 在方法中移动文件。也不要取消设置 %this->文件,只需分配空值。

基于您的类

/**
 * @ORMPrePersist()
 */
public function preUpload()
{
    if (null !== $this->file) {
        // do whatever you want to generate a unique name
        $this->path = uniqid().'.'.$this->file->guessExtension();
    }
}
/**
 * @ORMPostPersist()
 */
public function upload()
{
     if (null === $this->file) {
         return;
     }
     $this->file->move($this->getUploadRootDir(), $this->path);
     $this->file = null;
}

请记住,使用 move 应该在服务器上使用绝对路径,例如/var/www/my_app/web/storage/uploads/file.jpg 或 C:\www\my_app\web\storage\uploads\file.jpg(不要太担心斜杠方向,如果你只使用"/"它应该可以正常工作)

public static function getUploadRootDir(){
    return $_SERVER['DOCUMENT_ROOT'].'/'.$this->getUploadDir(); 
}  

最新更新