Symfony2-如何通过表单上传多个附件

Symfony2-如何通过表单上传多个附件,symfony,doctrine-orm,Symfony,Doctrine Orm,我有一个关于上传的symfony2问题。我使用的是教义,有两个实体,第一个叫做“问题”,有很多附件,第二个叫做“附件”。我使用的是一个动态数据解决方案,它使人们能够在“问题”表单上上传许多附件,并为每次上传都提供一个标题。当我保存“问题”时,也会保存相关的附件行,但只保存标题:(上传”方法永远不会捕获正在上传的实际文件。我正在学习symfony2 cookbook教程,想知道我是否错过了一个步骤?我有点迷路,而且在学习过程中很早,不知道如何调试这个问题 我的附件内容如下: namespace W

我有一个关于上传的symfony2问题。我使用的是教义,有两个实体,第一个叫做“问题”,有很多附件,第二个叫做“附件”。我使用的是一个动态数据解决方案,它使人们能够在“问题”表单上上传许多附件,并为每次上传都提供一个标题。当我保存“问题”时,也会保存相关的附件行,但只保存标题:(上传”方法永远不会捕获正在上传的实际文件。我正在学习symfony2 cookbook教程,想知道我是否错过了一个步骤?我有点迷路,而且在学习过程中很早,不知道如何调试这个问题

我的附件内容如下:

namespace WebConfection\ServiceDeskBundle\Entity;

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

/**
 * @ORM\Table(name="webconfection_servicedesk_attachment")
 * @ORM\Entity(repositoryClass="WebConfection\ServiceDeskBundle\Repository\AttachmentRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Attachment
{

    private $temp;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     protected $id;

    /**
     * @ORM\Column(type="string", length=150)
     */
    protected $title;    

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

    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $updated;

    /**
     * @ORM\ManyToOne(targetEntity="Issue", inversedBy="attachments")
     * @ORM\JoinColumn(name="issue_id", referencedColumnName="id")
     */
    protected $issue;

    /**
     * Constructor
     */
    public function __construct()
    {

        $this->setCreated(new \DateTime());
        $this->setUpdated(new \DateTime());
    }    

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        // check if we have an old image path
        if (is_file($this->getAbsolutePath())) {
           // store the old name to delete after the update
           $this->temp = $this->getAbsolutePath();
        } else {
            $this->path = 'initial';
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            $this->path = $this->getFile()->guessExtension();
        }
    }


    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->temp);
            // clear the temp image path
            $this->temp = null;
        }

        // you must throw an exception here if the file cannot be moved
        // so that the entity is not persisted to the database
        // which the UploadedFile move() method does
        $this->getFile()->move(
            $this->getUploadRootDir(),
            $this->id.'.'.$this->getFile()->guessExtension()
        );

        $this->setFile(null);
    }

    /**
     * @ORM\PreRemove()
     */
    public function storeFilenameForRemove()
    {
        $this->temp = $this->getAbsolutePath();
    }

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

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

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Attachment
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Attachment
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Attachment
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }
}

我有一个类似的问题,下面是我如何解决它的

不要介意我对symfony2的咆哮和愤怒,有时我需要在某个地方发泄;)

这可能不是你问题的具体答案,但可能会对你有所帮助

我在控制器中进行移动,当然我可能会将此功能移动到实体中,但是,嗯,它可以工作,我只需要上传一次功能,这会减少内存使用量,而不是将其放在实体中