0
votes

I have a symfony2 question on uploads. I am using doctrine and have two entities, the first called "Issue" that has many attachments and a second called "Attachment". I am using a dynamic data solution that enables people to upload many attachments on the "Issue" form and give each upload a title. When I save the "Issue" the associated attachment rows are also saved but only the title :( The actual files being uploaded are never captured by the "upload" method. I am following the symfony2 cookbook tutorial and wondered if I have missed a step? I'm a bit lost and very early in my learning curve and not sure how to debug this problem.

My Attachment entity reads as follows :

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;
    }
}
1

1 Answers

2
votes

I had a similar problem and here's how I solved it.

http://blog.icod.de/2013/04/17/symfony2-file-upload-with-related-entities/

don't mind my ranting and raging at symfony2, I need to vent somewhere on occasion ;)

it may not be a specific answer to your question but maybe it will help you

I'm doing the moving in the controller, I might of course move this functionality to the entity but meh, it works and I only need to upload functionality once and this reduces memory usage a bit instead of having it in the entity.

HTH