0

ファイルのアップロードから教義にパスを保存する際に問題があります。ここの例を使用しました: How to handle File Uploads with Doctrine

アップロードは機能しますが、ファイル パスがデータベースに保存されず、コールバックが機能しないと思います。少なくとも私のアップロードにはファイル拡張子がありません。

これが私のコントローラーです:

   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 IS\ClearanceBundle\Entity;

   use Doctrine\ORM\Mapping as ORM;
   use Symfony\Component\HttpFoundation\File\UploadedFile;
   use Symfony\Component\Validator\Constraints as Assert;

  /**
   * IS\ClearanceBundle\Entity\Document
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks
   */
  class Document
 {
 /**
  * @var string $name
  */
private $name;

/**
 *  @ORM\Column(type="string", length=255, nullable=true)
 */
public $path;

/**
 * @var integer $projectId
 */
private $projectId;

/**
 * @var integer $id
 */
private $id;

 /**
 * @Assert\File(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';
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
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();
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
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);
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
 }
}

データベースへのパスを保存する方法とデバッグする方法が本当にわかりません。

助けてくれてありがとう!

4

1 に答える 1

1

upload()メソッドを見てください。postPersist()があります。これは、エンティティの変更がDBに保存されないため、変更されたプロパティが現在の保存に保存されないことを意味します。

prePersistアノテーションを使用してメソッド内のパスを更新し、postPersistを使用してメソッド内のファイルを移動する必要があります。また、%this-> fileの設定を解除せず、null値を割り当てるだけです。

あなたのクラスに基づく

/**
 * @ORM\PrePersist()
 */
public function preUpload()
{
    if (null !== $this->file) {
        // do whatever you want to generate a unique name
        $this->path = uniqid().'.'.$this->file->guessExtension();
    }
}

/**
 * @ORM\PostPersist()
 */
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(don 'スラッシュ方向についてはあまり気にしないでください。「/」のみを使用すると、正常に機能するはずです)

public static function getUploadRootDir(){
    return $_SERVER['DOCUMENT_ROOT'].'/'.$this->getUploadDir(); 
}  
于 2012-07-02T12:26:53.003 に答える